101、编译错误:ld: library notfound for -lpods
当项目中使用了 cocoapods 时,经常出现此错误(通常是 release 的时候)。
这是由于 pod install 后,cocoapods 会创建一个新的 workspace。你必须关闭项目并重新打开。问题即可解决。
102、为什么 ios 的时间总是比真实时间慢8小时
例如,一个北京时间2014-4-4 22:00(字符串),需要转换成 nsdate。字符串转换成 nsdate 一般是通过 nsdateformatter 进行的。而在 ios 上 nsdate 以 gmt 时间存储,因此nsdateformatter会自动将字符串当前时区的本地时间处理,即将被转换的北京时间(字符串2014-4-4 22:00)换成 gmt 时间(2014-4-4 14:00)。如果直接把这个 nsdate(longlong,1970以来的秒数或毫秒数)传给服务器,服务器会把这个时间当成北京时间使用(实际上它却是gmt时间),这就导致时间差了8小时。
正确的做法是在这个 nsdate 的基础上加上时差。而时差的计算需要知道当前时区。[nstimezonesystemtimezone]可以得到当前时区(东8区),然后用 secondsfromgmtfordate: 方法可获得这个时区(东8区)的时差(以秒计)。代码如下:
nsdateformatter* df=[nsdateformatter new];
// [dfsetlocale:[nslocale currentlocale]];
df.dateformat=@"yyyy-mm-dd hh:mm";
nsdate* date=[dfdatefromstring:@"2014-4-4 22:00"];
nstimezone *zone =[nstimezone systemtimezone];
nsinteger interval = [zonesecondsfromgmtfordate: date];
nsdate *localedate =[date datebyaddingtimeinterval:interval];
nslog(@"%@",localedate);
103、禁止 uitableviewcontroller 中键盘弹出动画
tableviewcontroller 中内置了键盘弹出动画的代码,当单元格中的输入控件弹出软键盘时,tableview 会自动向上滚动。但这个功能有时候会带来大麻烦,因为有时候输入控件会被滚动到不可视的地方。由于我们无法修改框架的代码,所以这种情况下我们必须放弃使用 tableviewcontroller(子类化),而改用一般的uiviewcontroller+uitableview 代替。但有时候我们必须使用tableviewcontrller——例如想使用它的静态单元格,则可以通过下列方法解决。在uitableviewcontroller 子类中覆盖 viewwillappear 方法,禁用父类的 viewwillappear 行为。即不要调用[superviewwillappear:animated]一句:
-(void)viewwillappear:(bool)animated{
// override super method with don'tcall [super viewwillapper]
}
104、应该在什么时候使用 nscache
nscache 会自动根据内存压力释放其中的某个缓存对象(例如视图被销毁,或者缓存的对象实在太多)。因此 nscache 缓存的对象必须是可以重建的,例如这些对象——可以在需要时从网络上下载到的数据。否则,你不应当使用 nscache——因为对象不知道什么时候就会被销毁了。
因此,使用 nscache 时必须注意,如果检索的对象在 cache 中不存在,我们必须重建一个:
-(cachedobject)getcachedobject:(id)key{
id* obj=[nscacheobjectobjectforkey:key];
if (cb==nil) {
obj=[[cachedobjectalloc]init]; // recreate cached object
……
}
return obj;
}
105、pods 在 xcode5 上archive 的问题
问题描述:
archive 时出现如下错误(debug 时可能是正常的):
ld: library not found for -lpods
问题是由于 xcode5.x 现在会检测依赖项目的 architecture ,其设置必须和主项目一致,否则该依赖项目会被拒绝(即不会编译)。
解决办法:
在 pods 项目的所有 target 下,将他们的 architecture 设置为和主项目一致。
106、如何查看一个静态库支持的 architecture
使用“lipo -info 静态库文件”命令,例如:
lipo -info unrar4ios
然后终端会作如下显示:
architectures in the fat file: unrar4ios are: armv7 armv6 i386
107、项目中引入某些静态库会导致在 archive 时报“undefined symbols forarchitecture armv7s/arm64”错误
正如问题 105 所述。除了用问题 105 中的解决方法,还有一种解决办法。
首先查看该静态库的 architecture(参考问题106)。然后修改 scheme 为该静态库支持的 architecture。然后修改 buid settings 中的 build active architectureonly(仅编译为所选的架构),将值改为 yes。然后编译即可。
108、autolayout 下,uitableview 的高度不正确
autolayout 下,如果有导航栏,视图上的 uitableview 受 constraints 的限制,运行时高度被 constraints 重新设置为没有导航栏的高度。此时应该实现viewdidlayoutsubviews 方法,将 constraints 的影响排除:
- (void)viewdidlayoutsubviews {
_table.frame=cgrectmake(0,0,self.view.frame.size.width,self.view.frame.size.height);
}
109、如何修改默认返回按钮的title?
假设导航为:a视图-->b视图
如果要改变b视图返回到a视图的返回按钮的title,只需在a视图中使用如下代码:
self.navigationitem.backbarbuttonitem = [[uibarbuttonitem alloc]initwithtitle:@"返回" style:uibarbuttonitemstyleplain target:self action:nil];
b视图不用做任何操作。
110、有一个空对象,但它既不是 nil,也不是 null?
它是 nsnull。你可以打印这个对象(用 po 命令或者 nslog),则打印结果是“<null>”,而不是“(null)”(nil 对象)。
由于o-c 的集合对象中不允许插入空值(nil),而 nsnull 并不是 nil,所以就用nsnull 对象来表示这个集合为空(表示列表结束)。而且,与 nil 不同,发送消息给一个 nsnull 将导致异常。
nsnull具有唯一的方法:[nsnull null]你可以用它来测试一个对象是不是 nsnull:
bool isnsnull(id any){
return [any isequal:[nsnullnull]];
}
以上就是ios 开发百问(9)的内容。