0

我從下面一雷Wenderlich教程的plist這是在Objective C.編碼我掙扎,下面的代碼從目標C轉換爲斯威夫特填充表用數據填充表:從plist中

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
    NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)]; 
    NSArray* sortedCategories = [self.articleDictionary.allKeys sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 

     NSString *categoryName = [sortedCategories objectAtIndex:section]; 

     NSArray *currentCategory = [self.articleDictionary objectForKey:categoryName]; 

return [currentCategory count]; 
} 

當我轉換如下:

var sortDescriptor:NSSortDescriptor = NSSortDescriptor(key: nil, ascending: true, selector: "localizedCompare:") 

我得到一個編譯器錯誤呼叫

指出「額外的參數‘選擇’

,然後當我嘗試以下行我得到XCode暗示我使用:

let sortedCategories:NSArray = [articleDictionary.allKeys.sort(<#isOrderedBefore: (AnyObject, AnyObject) -> Bool##(AnyObject, AnyObject) -> Bool#>)] 

有什麼建議嗎?

回答

0

儘管出現錯誤消息(Swift的錯誤消息並非如此迅速),但問題似乎是將鍵傳遞給nil。如果你不需要的關鍵,那麼你應該使用sortedArrayUsingSelector:相反,

let keys: NSArray = articleDictionary.allKeys 
var sortedCategories = keys.sortedArrayUsingSelector("localizedCompare:") 

這也似乎工作,如果你傳遞一個空字符串(而不是零)的關鍵參數sortDescriptorWithKey:上行:選擇: