2014-12-04 39 views
-1

我試圖從Objective-C代碼的下面一行斯威夫特轉換:問題調用從斯威夫特與NSArrays和NSDictionaries的Objective-C API

- (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]」

我試圖改變周圍的類型,鑄造,一切我新手大腦可以想到但我保持克類似的錯誤。 contentModel對象是從下面的Objective-C代碼創建的,如果這有幫助的話:

@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 
+0

您能否詳細介紹一下字典和數組包含哪些類型?所以我可以編寫swift代碼。特別是什麼是內部主幹數組 – kap 2014-12-04 16:13:54

+0

這是調試器有什麼:_spine =(NSArray *)@「386 objects」,[0] =(_NSCFString *)@「ncx」,[0](_NSCFString *)。 – 2014-12-04 16:54:01

回答

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 
       } 
      } 
     } 
    } 
}