【发布时间】:2016-12-14 22:52:51
【问题描述】:
我有 2 个客观的 C 类:
- 1 类 scanDatabase(扫描数据库并将其放入可变数组)
- 2 类映射器(是数据库扫描“模型”的映射类)
在目标 C 中,这成功地扫描了数据库并将其放入一个可变数组中。使用映射类,我可以访问单个元素组(AlbumTitles),如下所示:
for (Mapper *mapper in scanResult) {
NSLog(@"%@", mapper.AlbumTitle);
}
一切正常,我可以从我的数组中返回单个元素,即如上所述,我只返回专辑标题。
然后我需要在 Swift 中使用该数组。我在我的 Swift 类中调用了目标 C,它再次运行良好并创建了数组。这是通过以下方式完成的:
let scanTable = ScanTable();
let scanMapper = Mapper();
scanTable.scanTableDo();
但是当我尝试检索像专辑标题这样的特定项目集时,就像我在上面的目标 C for 循环中所做的那样,我收到错误“scanMapper 不是类型”(scanMapper 是我的目标 C 映射器类的快速实例:
我尝试了两种不同的方法,但都有相同的错误:
for mapper: scanMapper in scanTable.scanResult {
print("\(mapper.AlbumTitle)")
}
for object in scanTable.scanResult as! [scanMapper] {
print("\(mapper.AlbumTitle)")
}
我可以使用一个客观的 C 类作为模型/映射器,但不确定是否需要在 Swift 中重新创建它。
我将包含映射器和 scanTable .h 和 .m 代码以备不时之需,以及桥接头:
映射器.h:
#import <Foundation/Foundation.h>
#import <AWSDynamoDB/AWSDynamoDB.h>
@interface Mapper : AWSDynamoDBObjectModel <AWSDynamoDBModeling>
@property (nonatomic, strong) NSNumber *SongID;
@property (nonatomic, strong) NSString *Artist;
@property (nonatomic, strong) NSString *SongURL;
@property (nonatomic, strong) NSString *Location;
@property (nonatomic, strong) NSNumber *UserRatings;
@property (nonatomic, strong) NSNumber *AVGUserRating;
@property (nonatomic, strong) NSString *Category;
@property (nonatomic, strong) NSString *PictureURL;
@property (nonatomic, strong) NSNumber *SongDuration;
@property (nonatomic, strong) NSString *SongTitle;
@property (nonatomic, strong) NSNumber *AVGMusicianRating;
@property (nonatomic, strong) NSString *AlbumTitle;
@end
映射器.m
#import <AWSDynamoDB/AWSDynamoDB.h>
#import "Mapper.h"
@implementation Mapper
+ (NSString *)dynamoDBTableName {
return @"Songs";
}
+ (NSString *)hashKeyAttribute {
return @"SongID";
}
@end
ScanTable.h:
#import <Foundation/Foundation.h>
#import <AWSDynamoDB/AWSDynamoDB.h>
@interface ScanTable : NSObject
- (void) scanTableDo;
@property (nonatomic, strong) NSMutableArray *scanResult;
@end
ScanTable.m
#import "ScanTable.h"
#import "Mapper.h"
@implementation ScanTable
- (void) scanTableDo {
AWSDynamoDBObjectMapper *dynamoDBObjectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
AWSDynamoDBScanExpression *scanExpression = [AWSDynamoDBScanExpression new];
scanExpression.limit = @10;
[[dynamoDBObjectMapper scan:[Mapper class]
expression:scanExpression]
continueWithBlock:^id(AWSTask *task) {
if (task.error) {
NSLog(@"The request failed. Error: [%@]", task.error);
}
if (task.exception) {
NSLog(@"The request failed. Exception: [%@]", task.exception);
}
if (task.result) {
AWSDynamoDBPaginatedOutput *paginatedOutput = task.result;
NSMutableArray *scanResult = [[NSMutableArray alloc] initWithArray:paginatedOutput.items]; //// ADDED /////
for (Mapper *mapper in scanResult) {
NSLog(@"%@", mapper.AlbumTitle);
}
}
return nil;
}];
}
@end
//编辑添加桥接头//
MySampleApp-Bridging-Header.h:
//
// MySampleApp-Bridging-Header.h
// MySampleApp
#import "ScanTable.h"
#import "Mapper.h"
#import "Hello World.h"
感谢您的帮助
【问题讨论】:
标签: objective-c arrays swift types