iOS 开发百问(10)

黄舟
黄舟 原创
2023-03-05 06:54:02 791浏览

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)的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。