使用NSArrays和NSDictionaries从Swift调用Objective-C API的问题

时间:2014-12-04 16:05:07

标签: ios objective-c swift nsarray nsdictionary

我正在尝试将以下代码行从Objective-C转换为Swift:

- (void)updateContentForSpineIndex:(NSUInteger)currentSpineIndex
{
    NSString *contentFile = self.contentModel.manifest[self.contentModel.spine[currentSpineIndex]][@"href"];

以下是我将其转换为:

func updateContentForSpineIndex(currentSpineIndex: Int)
{

    let contentFile = NSString(self.contentModel.manifest(self.contentModel.spine(currentSpineIndex))("href"))

我收到错误,'(Int) - > $ T7'与'[AnyObject]'

不同

我已经尝试改变周围的类型,转换,以及我的新手大脑可以想到的所有内容,但我一直遇到类似的错误。如果有帮助,则从以下Objective-C代码创建contentModel对象:

@interface KFEpubContentModel : NSObject

@property (nonatomic) KFEpubKitBookType bookType;
@property (nonatomic) KFEpubKitBookEncryption bookEncryption;

@property (nonatomic, strong) NSDictionary *metaData;
@property (nonatomic, strong) NSString *coverPath;
@property (nonatomic, strong) NSDictionary *manifest;
@property (nonatomic, strong) NSArray *spine;
@property (nonatomic, strong) NSArray *guide;

@end

1 个答案:

答案 0 :(得分:0)

此。我假设清单字典包含字符串键值的字典

class KFEpubContentModel {
    var manifest: Dictionary<String, Dictionary<String, String>>?
    var spine: Array<String>?
}

var contentModel = KFEpubContentModel()
func updateContentForSpineIndex(currentSpineIndex: Int) {
    if let s = contentModel.spine {
        let currentSpine = s[currentSpineIndex]
        if let m = contentModel.manifest {
            let currentManifest = contentModel.manifest?[currentSpine]
            if let cm = currentManifest {
                let contentFile = cm["href"]
                if let cf = contentFile {
                    //do stuff
                }
            }
        }
    }
}