最近工作涉及到 iOS,使用过程中经常遇到不少错误,特此记录,以备查询。
所用环境为
- Max OS 10.12.6
- Xcode 8.3.3
- iOS 10.3.3
0.
出现错误后,在红色错误信息处,“右键 - Reveal in log - Detail Message”
会看到详细的出错信息,一般能从这里看到具体的问题是什么,然后采取相应的措施。
简单来说,就是两步“Google error message”、“Copy from StackOverFlow” 。
有时候一种现象,背后会有多种原因,于是就需要多试几种方法。总会解决的:).
由于本人对 iOS 完全是一知半解,如有不对之处,还望指出,不吝感激。
1. Applications are expected to have a root view controller at the end of application launch
iOS 从某个版本之后每个 window都要设置一个 RootViewController。否则在运行时便会挂掉。
检查你的 AppDelegate.m
文件(或者是 XXXXAppDelegate.m
文件)的 didFinishLaunchingWithOptions
函数。
如果函数中是如下形式: [window addSubview:[someController view]];
则将其改为 [self.window setRootViewController:someController];
如果修改之后,仍报同样的错误。说明你程序中有不止一个 window ,而你并没有为每个 window 都设置了 RootViewController 。全部设置相应的 RootViewController 后即可解决。
需要注意的是 iOS 会默认根据 xib/storyboard 生成一个 window (本人对 iOS 并不是很熟,如果这里说法有误,欢迎指正)。所以有时候,你只有一个 window ,并且设置了 RootViewController 后还报错,则需要将类似如下代码的创建 window 的代码删除。
1 | self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; |
2. 模拟器上正常运行,真机测试时报错
"_WebPGetFeaturesInternal", referenced from:
"_png_get_io_ptr", referenced from:
"_TIFFGetField", referenced from:
"_jpeg_read_header", referenced from:
"_TIFFClientOpen", referenced from:
"_FT_Get_Glyph", referenced from:
“右键 - Reveal in log - Detail Message” ,可看到
Undefined symbols for architecture arm64
这种情况通常是程序在 Build 的过程中没有将用到的所有 Library 链接进去。
通过“选中项目 - TARGETS - Build Phases - Link Binary With Libraries” ,
并根据上面报错的名字,将相应的 Library 添加进去即可。
我这里是因为用了 cosos2d ,所以将
/PATH_TO_COSOS2D/cocos2d/external/XXX_MODULE/prebuilt/ios/libXXX[_arm64].a
包含进去即可。
将库添加后,如果还报错,则需在“项目 - Build Setting - Library Search Paths” 中添加“刚才添加的库”的目录。(在后面选择 recursive 可使编译器搜索指定目录及其子目录。)
3. unknow type name ‘namespace’
等诸如 C++ 中的语法,编译时报错。
通常报错的文件是 .h 文件,而包含该文件的是 .h 或 .m 文件。将 .m 后缀修改成 .mm 后缀,编译器便会将该文件当做 Objective-C++ 文件来编译。
或者选中文件后,在右侧的 “Identity and Type - Type” 中选择 “Objective-C++ File” 也可达到同样的效果。
4. library not found for -lcrt1.3.1.o
http://blog.csdn.net/skylin19840101/article/details/56008803
crt1.3.1.o is a library that was included in older iOSes (and thus, their SDKs) but is no longer present in newer SDKs. However, when the project’s Deployment Target is set to an older ios (<6.0, as @Sandy has found), Xcode still tries linking against it.
在 iOS6 以及更高的版本中,crt1.3.1.o 已经没有了。
修改支持的最低 iOS 系统版本
在 “项目 - TARGETS - General -> Deployment Info -> Deployment Target” 中,把 5.0(及以下的)改为 6.0(及以上的)
5. Use of undeclared identifier ‘glDeleteTextures’
https://stackoverflow.com/questions/24024764/errors-showing-for-oes-opengl-statements-in-xcode-6-for-ios8
添加如下代码 #import <OpenGLES/ES2/glext.h>
6. ‘release’ is unavailable: not available in automatic reference counting mode
Xcode 设置中开启了 ARC , 但是代码仍然进行了内存释放等操作。
要么关闭 ARC , 要么去掉代码中的释放内存相关的代码。
7. clang:error: -fembed-bitcode is not supported on versions of iOS prior to 6.0
或者
1 | /PATH_TO_COSOS2D/cocos2d/external/freetype2/prebuilt/ios/libfreetype.a(ftbase.o)' |
这都是因为新的 iOS 不支持 bitcode 选项。
将 “项目 - TARGETS - Build Setting - Enable Bitcode” , 改为 no 即可。