iOS第四天

来源:互联网 发布:莱州网络购物 编辑:程序博客网 时间:2024/05/17 07:15

整数浮点数如何加入到array中去?

==与NSArray提供的isEqualToArray:有什么区别?

字符串@“234”转为整数

实现一个utility类,提供计算阶乘计算ab次方

在其他,.m中使用Utility

类是否支持多继承?

7  完善parent类实现init方法,init类初始化自己的成员完善Child类的fun方法,在fun中调用父类fun方法(super关键字)

  完善Parent/Child类实现 dealloc方法,在 dealloc中释放成员变量

p autorelease]释放的基本原理是什么?

9  @property属性是否对外公开的?

10  @property属性支持哪些 modifier?

11  assign/copy/retain的区别?

12添加到Array对对象引用计数的影响?

    set/dictionary是否也有影响?

13. ARC中内存释放的基本原理是什么?
14. __weak__unsafe_unretained区别?示例说明







NSNumber


用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类型值。



1.整数浮点数如何加入到array中去?

@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类,提供计算阶乘计算ab次方

Utility.h

#import <Foundation/Foundation.h>


@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



类是否支持多继承?


 在OC中类不支持多继承,同Java一样,与C++不同


7  完善parent类实现init方法,init类初始化自己的成员完善Child类的fun方法,在fun中调用父类fun方法(super关键字)

  完善Parent/Child类实现 dealloc 方法,在 dealloc 中释放成员变量


parent.h


#import <Foundation/Foundation.h>


@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




“+” 方法  [类名  函数名]
“-”方法   [实例化  函数名]