標籤:
121、如何將字典/數群組轉換為字串?
NSString* id2json(id dicOrArr){
NSError *error;
NSData *jsonData =
[NSJSONSerialization
dataWithJSONObject:dicOrArr
options:NSJSONWritingPrettyPrinted // Pass 0 if you don‘t care about thereadability of the generated string
error:&error];
if (! jsonData) {
DLog(@"Got an error:%@", error);
return nil;
} else {
NSString *jsonString =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
return jsonString;
}
}
122、Xcode 6.1 GM,AudioToolbox 無法使用
通常,我們在程式中習慣用 AudioToolbox 來播放較短的聲音效果,例如這段代碼,在 Xcode 6.0 中是可用的,但在 Xcode 6.1 GM 上會導致一個編譯錯誤:
func playSystemSound(soundName:String){// Tink.caf
letsoundPath:String="/System/Library/Audio/UISounds/"+soundName
var soundID:SystemSoundID = 0
ifNSFileManager.defaultManager().fileExistsAtPath(soundPath) {
let soundURL =NSURL(fileURLWithPath: soundPath)
AudioServicesCreateSystemSoundID(soundURL as CFURL, &soundID)
AudioServicesPlaySystemSound(soundID)
}
}
只需將 AudioServicesCreateSystemSoundID(soundURL as CFURL, &soundID) 替換為:
AudioServicesCreateSystemSoundID(soundURL, &soundID)
123、升級為 Yosemite 後,Xcode6 無法運行模擬器
升級為 Yosemite 後,Schema 中的所有模擬器消失,如所示,模擬器一欄僅剩下 iOS Device :
此時,App 無法在模擬器上運行。
解決方案為,開啟模擬器(方法:Xcode -> Open Developer Tool -> iOS ),此時會彈出一個 Unable to determine device 的錯誤,不用管,點擊 OK。在模擬器菜單,Hardware -> Device ->Manage Devices…,彈出裝置列表。點擊左下角的 + 按鈕,添加所需要的模擬器,然後點擊 Create即可,如所示:
124、整合Alamofire時出現錯誤:only supported on iOS 8.0 andlater
將Alamofire的deployment target設定為和主專案的一樣。
125、錯誤:must register a nib or a class for the identifier
當使用編程方法構建UITableViewCell時,需要註冊該UITableViewCell的xib。在 viewDidLoad 方法中加入下句:
[self.yourTableViewName registerNib:[UINibnibWithNibName:@"YourNibName" bundle:nil]
forCellWithReuseIdentifier:@"YourIdentifierForCell"];
如果UITableViewCell未使用xib,但該UITableViewCell有自訂的.m/.h檔案,則使用下句替代:
[self.tableView registerClass:[UITableViewCell class]forCellReuseIdentifier::@"Cell"];
126、錯誤Undefined symbols for architecture armv7
如果你在真機調試出現這個錯誤,在模擬器下沒有這個錯誤。說明有某個lib庫只有x86版本,而缺少armv7/armv6版本。查看錯誤所指向的庫,用lipo –info 命令檢查該.a檔案的二進位資訊。
如果缺少armv6/armv7版本的.a檔案,請重新編譯一個armv7/armv6的.a檔案添加到工程中。
還可以用lipo –create –output命令將兩個版本的.a檔案融合成一個通用版.a檔案放到工程中。
此外,可能還需要檢查Library Search Path設定,看.a檔案的路徑指向是否正確。
127、錯誤Type ‘String‘ does not conform to protocol ‘IntervalType‘
當switch語句中對String? 進行匹配時出現此錯誤。例如:
switch item.name {
case "到達現場時間":
cellItems.append(item)default:
break
}
修改為如下語句可解決此錯誤:
if let name = item.name{
switch name {
case "到達現場時間":
cellItems.append(item)default:
break
}
}
128、pod update時經常出現錯誤:Attempt to read nonexistent folder
cocoapod伺服器被牆了,請使用代理或者VPN(推薦VPN)。
129、ViewController類聲明出現錯誤:initializer ‘init(coder:)‘must be provided by a subclass of UIViewController
在你的ViewController中有某個屬性聲明時未賦予初值,而且也沒有標記為可空。例如 varkPickerViewSize:CGSize,可修改為var kPickerViewSize:CGSize?即可消除此錯誤
130、Swift中如何取得Dictionary的所有key?
在NSDicationary中可以使用allKeys屬性,在Dictionary中則使用keys屬性:
let array:[String] = [String](dictionary.keys)
iOS 開發百問(10)