大家好,我是你的好朋友思创斯。今天说一说ios第四天_ios runtime,希望您对编程的造诣更进一步.
1 整数浮点数如何加入到array中去?
2 ==与nsarray提供的isequaltoarray:有什么区别?
3 字符串@“234”转为整数
4 实现一个utility类,提供计算阶乘‘计算a的b次方
5 在其他,.m中使用utility
6 类是否支持多继承?
7 完善parent类实现init方法,init类初始化自己的成员完善child类的fun方法,在fun中调用父类fun方法(super关键字)
完善parent/child类实现 dealloc方法,在 dealloc中释放成员变量
8 [p autorelease]释放的基本原理是什么?
9 @property属性是否对外公开的?
10 @property属性支持哪些 modifier?
11 assign/copy/retain的区别?
12添加到array对对象引用计数的影响?
set/dictionary是否也有影响?
13. arc中内存释放的基本原理是什么?
14. __weak与__unsafe_unretained区别?示例说明
用nsnumber类来用面向对象的方法处理数字。如果你只需要简单的数字(而不是对象),用nsinterger类来操作有符号数(正数或者负数),用nsuinterger类来操作无符号数(正数或0),用cgfloat类和double来操作浮点数。
numberwithint:将一个整数值封装成一个nsnumber实例;
numberwithunsignedint:将一个无符号整数值(正数或0)封装成一个nsnumber实例;
numberwithfloat:将一个浮点数封装成一个nsnumber实例;
numberwithdouble:将一个double类型的数封装成一个nsnumber实例;
intvalue:从调用该函数的nsnumber实例中返回一个整型nsiteger类型值。
unsignedintvalue:从调用该函数的nsnumber实例中返回一个无符号整型nsiteger类型值。
floatvalue:从调用该函数的nsnumber实例中返回一个浮点数cgfloat类型值。
doublevalue:从调用该函数的nsnumber实例中返回一个双精度double类型值。
@implementation appdelegate
– (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
nsnumber *a=[nsnumbernumberwithfloat:3.890]; //nsnumber的使用
nsnumber*b=([nsnumbernumberwithinteger:7]);
nsarray *array=[[nsarrayalloc]initwithobjects:a,b,nil];
nsarray *array2=[[nsarrayalloc]initwithobjects:a,b,nil];
nslog(@”%@”,array);
// override point for customization after application launch.
return yes;
}
2013-07-25 19:54:33.456 fourday[706:11303] (
“3.89”,
7
)
2==与nsarray提供的isequaltoarray:有什么区别?
@implementation appdelegate
– (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
nslog(@”%d”,array2==array); / /等号的值为0,就是说array2与array不相等,比较的是地址
nslog(@”%d”,[array isequaltoarray:array2]); //isequaltoarray:的值为1,就是说两数组的值相同,比较的是数组的内容
// override point for customization after application launch.
return yes;
}
2013-07-25 19:54:33.457 fourday[706:11303] 0
2013-07-25 19:54:33.457 fourday[706:11303] 1
3.字符串@“234”转为整数
@implementation appdelegate
– (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
nsstring *str=@”234″;
nslog(@”%d”,[strintvalue]);
// override point for customization after application launch.
return yes;
}
2013-07-25 19:54:33.458 fourday[706:11303] 234
4. 实现一个utility类,提供计算阶乘‘计算a的b次方
utility.h
#import
@interface utility :nsobject
-(double)fun:(double) a another:(double)b;
@end
utility.m
#import “utility.h”
@implementation utility
-(double)fun:(double)a another:(double)b
{
double c=pow(a, b);
return c;
}
@end
appdelegate.m
@implementation appdelegate
– (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
utility *r=[[utility alloc]init];
double m=[rfun:3.0 another:2.0];
nslog(@”%f”,m);
// override point for customization after application launch.
return yes;
}
2013-07-25 19:54:33.458 fourday[706:11303] 9.000000
6 类是否支持多继承?
在oc中类不支持多继承,同java一样,与c++不同
7 完善parent类实现init方法,init类初始化自己的成员完善child类的fun方法,在fun中调用父类fun方法(super关键字)
完善parent/child类实现 dealloc 方法,在 dealloc 中释放成员变量
parent.h
#import
@interface parent :nsobject
@property (assign)nsstring *c;
-(id)init;
-(void)fun:(int)b;
-(void)dealloc;
@end
parent.m
#import “parent.h”
@implementation parent
-(id)init
{
if(self =[super init])//super父类
{
//default value
self.c=[[nsstringalloc]initwithformat:@”this is init”];
}
return self;
} //转到父类定义的init
-(void)fun:(int)b
{
b=5;
nslog(@”%@”,@”this is fun parent”);
}
-(void)dealloc
{
[super dealloc];
[self.crelease];
}
@end
child.h
#import “parent.h”
@interface child : parent
-(void)fun:(int)a;
@end
child.m
#import “child.h”
@implementation child
-(void)fun:(int)a
{
a=1;
nslog(@”%@”,@”this is child”);
[superfun:10];
}
@end
appdelegate.m
@implementation appdelegate
– (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
parent *p1=[[parentalloc]init];//在调用init是首先调转到父类的定义init中,执行父类的init定义
child *p2=[[childalloc]init];
[p1 fun:10];
[p2 fun:7];
nslog(@”%@”,p1.c);
[p1 release];//在调用时首先转到父类的dealloc函数,先释放父类的成员变量,再将父类释放掉
nslog(@”%@”,@”this “);
// override point for customization after application launch.
return yes;
}
2013-07-25 19:54:33.460 fourday[706:11303] this is fun parent
2013-07-25 19:54:33.461 fourday[706:11303] this is child
2013-07-25 19:54:33.461 fourday[706:11303] this is fun parent
2013-07-25 19:54:33.462 fourday[706:11303] this is init
2013-07-25 19:54:33.462 fourday[706:11303] this
12 添加到array对对象引用计数的影响?
set/dictionary是否也有影响?
@implementation appdelegate
– (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
nsstring *str2=[nsstring stringwithformat:@”this is”];
int count=[str2retaincount];
nslog(@”%d”,count);
nsset *s=[[nsset alloc]initwithobjects:str2,nil];
count=[str2retaincount];
nslog(@”%d”,count);
nsdictionary *dic3=[nsdictionary dictionarywithcontentsoffile:str2];
count=[str2retaincount];
nslog(@”%d”,count);
// override point for customization after application launch.
return yes;
}
2013-07-25 19:54:33.459 fourday[706:11303] 1
2013-07-25 19:54:33.459 fourday[706:11303] 2
2013-07-25 19:54:33.460 fourday[706:11303] 2
14. __weak
与
__unsafe_unretained
区别?示例说明
@implementation appdelegate
– (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
nsmutablestring *p=[nsmutablestring stringwithformat:@”%@“,@”test”]; //先创建一个对象,再进行复制进行了两次操作,所以值比下一个大一
nsmutablestring __unsafe_unretained *str3=p;
nsmutablestring __weak *str2=p;
nslog(@”%ld”,cfgetretaincount((__bridgecftyperef)str2));
nslog(@”%ld”,cfgetretaincount((__bridgecftyperef)str3));
nslog(@”%@”,str2);
nslog(@”%@”,str3);
__strong nsstring *p2=[[nsstring alloc]initwithformat:@”%@“,@”text”];//申请空间,直接赋值,没有中间创建对象的过程,所以比上一个少一
__weak nsstring *str4=p2;
__unsafe_unretained nsstring *str5=p2;
nslog(@”%ld”,cfgetretaincount((__bridgecftyperef)str4));//将强类型赋给弱类型和__unsafe_unretained类型,
nslog(@”%ld”,cfgetretaincount((__bridgecftyperef)str5));//然后将强类型赋空值,弱类型地址改变为零,__unsafe_unretained类型不变
p2=nil;
nslog(@”%@”,str4);
nslog(@”%@”,str5);
// override point for customization after application launch.
return yes;
}
2013-07-25 19:50:10.781 oneday[625:11303] 3
2013-07-25 19:50:10.782 oneday[625:11303] 3
2013-07-25 19:50:10.784 oneday[625:11303] 2
2013-07-25 19:50:10.784 oneday[625:11303] 2
p2nsstring *0x00000000
str4nsstring *0x00000000
str5__nscfstring *0x071900a0
“ ” 方法 [类名 函数名]
文章由思创斯整理,转载请注明出处:https://ispacesoft.com/128499.html