iOS开发_Tips(2)

1、UIImage转换为NSData

UIImage *aimage=[UIImage imageNamed:@"layne.png"];
NSData *imageData = UIImagePNGRepresentation(aimage);

UIImage *aimage=[UIImage imageNamed:@"layne.png"];
NSData *imageData = UIImageJPEGRepresentation(aimage,1.0);

UIImageJPEGRepresentation包含一个压缩系数,用于指定图片压缩程度,0(most)~1.0(least)

2、Xcode中使用的frameworks路径

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/lib

eg:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/lib/libsqlite3.dylib

从查看了具体的framework可知,libsqlite3.0.dylib是libsqlite3.dylib的替身,即添加3.0还是3都是添加的同一个文件。

3、不常用的三个OC关键字:

(1)@compatibility_alias:用于设置类的别名.

eg:

@compatibility_alias AliasClassName OriginalClassName

注:OriginalClassName可以是系统类,如UITextField

(2)@encode:用于获得某个类型的字符串

eg:

char *var1=@encode(int);
char *var2 = @encode(float);
char *var3 = @encode(id);
char *var4 = @encode(BOOL);
char *var5= @encode(NSString *);
printf("1:%s\n 2:%s\n 3:%s\n 4:%s\n 5:%s\n",var1,var2,var3,var4,var5);

输出结果:
1:i
2:f
3:@
4:B
5:@

使用:
CGRect rect = CGRectMake(0, 0, 100, 100);
NSValue *val = [NSValue value:&rect withObjCType:@encode(CGRect)];

(3)@defs:用于返回一个Objective-C类的struct结构,这个struct与原Objective-C类具有相同的内存布局



4、viewWillUnload和viewDidUnload这俩函数在iOS6+不会再被调用,已经deprecated了。



5、iOS开发工具汇总。(详见唐巧的技术博客http://www.devtang.com/blog/2013/06/16/ios-dev-tool-app-store-tool/ )

用户行为统计工具:友盟,Flurry,Google Analytics等

App Store销售分析工具:App annie

App crash收集工具:Crashlytics

App测试发布工具:Test Flight



6、@encode:可用来返回某一类型的C字符串(char *)的表示

eg:

@encode(int)------"i";
@encode(float)------"f";
@encode(double)------"d"
@encode(BOOL)------"B"
@encode(char *)------"*"
@encode(id)------"@"//实例对象
@encode(NSString *)------"@"//实例对象
@encode(SEL)------":"//selector
@encode(void)------"v"
@encode(char)------"c"
@encode(int *)------"^i"
@encode(float *)------"^f"
@encode(double *)------"^d"

一般可用来判断NSValue和NSNumber中包含的具体数据类型:

eg:

NSNumber *num=[NSNumber numberWithInt:1];
const char *ty=[num objCType];
if(strcmp(ty, @encode(int)) == 0){
    //is int
}else if (strcmp(ty,@encode(float)) == 0){
    //is float
}else if......



7、一个方法 Method,其包含一个方法选标 SEL �C 表示该方法的名称,一个types �C 表示该方法参数的类型,一个 IMP  - 指向该方法的具体实现的函数指针。


8、category为何只能加方法不能加实例变量?

答:因为对象在内存中的排布可以看成一个结构体,该结构体的大小并不能动态变化。所以无法在运行时动态给对象增加成员变量。

相对的,对象的方法定义都保存在类的可变区域中。Objective-C 2.0并未在头文件中将实现暴露出来,但在Objective-C 1.0中,我们可以看到方法的定义列表是一个名为 methodLists的指针的指针。通过修改该指针指向的指针的值,就可以实现动态地为某一个类增加成员方法。这也是Category实现的原理。同时也说明了为什么Category只可为对象增加成员方法,却不能增加成员变量。(引自唐巧的技术博客)



9、iOS开发中使用过的网络封包分析工具-Charles,详情参见唐巧的技术博客:http://www.devtang.com/blog/2013/12/11/network-tool-charles-intr/


10、Xcode安装插件XToDo之后,ctrl+t调出todolist窗口。


11、app内跳转到appStore的方式:


[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/wu-you-jing-ying/id956393794?mt=8" ]];


[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/wu-you-jing-ying/id956393794?mt=8"]];

如果使用

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.apple.com/cn/app/wu-you-jing-ying/id956393794?mt=8"]]

则会跳转到iTunes Store

注意

"https://itunes.apple.com/cn/app/wu-you-jing-ying/id956393794?mt=8"

是“https”,这个地址可以通过电脑上的itunes获取,如图在app图标单击鼠标右键出现拷贝链接:

wKioL1X-IGfQc20sAAL5IFTxdt8375.jpg

12、Xcode下载的simulator路径:

/Users/<username>/Library/Caches/com.apple.dt.Xcode/Downloads

这里面是.dmg文件。

已经安装的simulator的路径如下:

/Users/<username>/Library/Developer/CoreSimulator/Devices

这个路径下会有好多长字符串命名的文件夹,如0AC1A21B-E05D-40CA-B553-20C07AE0DB34,每个文件夹对应一种simulator,文件夹里有一个plist文件标明此simulator的类型(iphone4/5/6 和ios6/7/8)。


13、UIView的init方法对实例进行初始化,实际上会自动调用initWithFrame方法,frame参数为(0,0,0,0)。


14、使用set函数对CGContext进行设置,如下:

CGContextRef context = UIGraphicsGetCurrentContext(); 
UIColor *fillColor=[UIColor blackColor];
[fillColor set];//设置填充色
CGContextFillEllipseInRect(context, CGRectMake(xOffset, yOffset, _measuredIndicatorWidth, _measuredIndicatorHeight));//from SMPageControl.m


15、ios常用的几个数学函数

(1)四舍五入

float roundf(float);
double round(double);

(2)向上取整

float ceilf(float);
double ceil(double);

(3)向下取整

float floorf(float);
double floor(double);

(4)绝对值

float fabsf(float);
double fabs(double);

(5)幂运算

float powf(float, float);
double pow(double, double);

(6)开平方

float sqrtf(float);
double sqrt(double);

(7)取余数(%)

float fmodf(float, float);
double fmod(double, double);

(8)整数余数一起取

float modff(float, float *);
double modf(double, double *);
eg:
    CGFloat t=3.23419875;
    CGFloat zheng=0;
    double xiao=modf(t,&zheng);
 结果:zheng=3.000000
      xiao=0.234199


16、Xcode开启警告的策略:-Wall("所有",实际为大部分的严重警告,而非全部)和-Wextra(额外警告)。方法如下:

Build Settings->Apple LLVM 6.1 -Custom Compiler Flags->Other C Flags添加-Wall(和-Wextra)。一般-Wall就可以了。


17、OC Runtime及消息转发

http://www.csdn.net/article/2015-07-06/2825133-objective-c-runtime/1


18、(1)设置View为nil是要先将其removeFromSuperView,才能激发dealloc。

        (2)如果在NSNotificationCenter注册了通知,那么一定要在dealloc中remove掉,否则一旦此对象被置为nil,通知产生时会crash。


19、如果发现touchesEnded没有调用,而点击时touchesCancelled却调用了,很有可能是当前View或其superview上添加了手势,即UIGestureRecognizer,iOS将touch事件当成了手势进行处理,导致点击按钮或是链接失效。




你可能感兴趣的:(sqlite,Runtime,数据类型转换)