ios 第1,2,3天

来源:互联网 发布:黄黄的软件 编辑:程序博客网 时间:2024/05/22 10:44

第三天  手动释放内存与ARC自动释放内存 


手动释放内存

parent.h

#import <Foundation/Foundation.h>


@interface parent :NSObject

-(void)fun:(int)a;

@property(assign,nonatomic)NSMutableString *c;

@property(copy,nonatomic)NSMutableString *b;



@end



AppDelegate.m

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


{

  

    parent *p1=[[parent alloc]init];

    p1.c=str;

    p1.b=str;

         int count=[p1.c retainCount];

    int count2=[p1.b retainCount];

    NSLog(@"%d",count);

    NSLog(@"%d",count2);  

   [p1.c retain];

   [p1.b retain];

    count=[p1.c retainCount];

    NSLog(@"%d",count);

   [p1.c release];

    count=[p1.c retainCount];

    NSLog(@"%d",count);


    return YES;

}


2013-07-24 10:44:49.176 OneDay[1056:11303] 1

2013-07-24 10:44:49.176 OneDay[1056:11303] 1

2013-07-24 10:44:49.177 OneDay[1056:11303] 2

2013-07-24 10:44:49.177 OneDay[1056:11303] 1



手动释放变为自动释放 

点击文件 在project;和targets中“command +f” 搜索auto,更改 Objective-C Automatic Reference .....更改为yes.


自动释放

   

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


{


   NSMutableString *p=[NSMutableString stringWithFormat:@"%@",@"test"];

   NSMutableString __strong *str=p;  //     构造开始的时候为一在赋值时p被加一,所以初始时p2

   NSMutableString __weak *str2=p;   //  在两个weak相当于一个strong

   NSLog(@"%ld",CFGetRetainCount((__bridgeCFTypeRef)str));

   NSLog(@"%ld",CFGetRetainCount((__bridgeCFTypeRef)str2));


// Override point for customization after application launch.

   return YES;

}



NSArray

NSArray 是不可变的,NSMutableString是可变的

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


{


   NSMutableString *A=[NSMutableString stringWithFormat:@"%@",@"This is A"];   //第一步构建多个NSMutableString

   NSMutableString *B=[NSMutableString stringWithFormat:@"%@",@"This is B"];

   NSMutableString *C=[NSMutableString stringWithFormat:@"%@",@"This is C"];

   NSMutableArray *test=[[NSMutableArray alloc] initWithObjects:A,B,C,nil];  //第二步将构造的NSMutableString添加到数组中

   int Count=[C retainCount];                //求出了对应AretainCount

   NSLog(@"%d",Count);                      

   [test removeLastObject];                  //把数组中最后一个对象移出

   Count=[C retainCount];                    //求除对应的C的retainCount

   NSLog(@"%d",Count);


 return YES;

}



2013-07-24 15:54:16.284 OneDay[744:11303] 2

2013-07-24 15:54:16.284 OneDay[744:11303] 1


nil 在构建数组时在最后面必须加上
NSMutableString  可以进行操作




字典

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


{


       NSArray *dic1=[[NSArray alloc] initWithObjects:@"One ",@"Two",@"three",nil];//构建两个NSArray的数组

    NSArray *dic2=[[NSArray alloc] initWithObjects:@"1",@"2",@"3",nil];

   NSDictionary *dic3=[NSDictionarydictionaryWithObjects:dic1forKeys:dic2];//将这两个数组放到字典中,构成字典

    for(id keyInDictionary in[dic3 allKeys])//dic3字典中的所有关键字

    {

        id value=[dic3 objectForKey:keyInDictionary];//从关键字中取出值

        NSLog(@"key=%@objectForkey=%@",keyInDictionary,value);//从字典中输出所有的值

    }


return YES;

}


2013-07-24 10:55:25.336 OneDay[1673:11303] key=1,objectForkey=One 

2013-07-24 10:55:25.336 OneDay[1673:11303] key=2,objectForkey=Two

2013-07-24 10:55:25.337 OneDay[1673:11303] key=3,objectForkey=three



第一天   建立两个函数

添加文件和熟悉界面


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


{

    NSString *simpleString=@"This is a simple string";//创建字符串

    NSString *simple=[simpleString substringToIndex:(3)];//取出前三个字符

   NSArray *array =[[NSArray alloc] initWithObjects:@"One",@"Two",@"three",nil];//构件字符数组

    NSLog(@"%@",array);

    parent *p1=[[parent alloc]init];//父类实例化

    parent*p2=[[Two alloc]init];//子类实例化

    NSLog(@"%@",p1.c);

    [p1 fun:3];//父子类调用函数

    [p2 fun:5];


    // Override point for customization after application launch.

   return YES;

}


parent.h

#import <Foundation/Foundation.h>


@interface parent : NSObject

-(void)fun:(int)a;

@property(nonatomic)NSMutableString *c;

@property(nonatomic)NSMutableString *b;



@end


parent.m

#import "parent.h"


@implementation parent

-(void)fun:(int)a

{

    

    NSLog(@"I am parent");

}

@end




child.h

@interface child: parent

-(void)fun:(int)b;

@end


child.m

@implementation child

-(void)fun:(int)b{

    NSLog(@"I am child");

}

@end



更换开机时的图片,更改在文件下面的png文件,主界面随之更换

添加按钮,找到相应控件,拖拽


parent.h

#import <Foundation/Foundation.h>


@interface parent : NSObject

-(void)fun:(int)a;

@property(assign,nonatomic)NSMutableString *c;

@property(copy,nonatomic)NSMutableString *b;



@end



@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


{


 parent *p1=[[parent alloc]init];

    NSMutableString *str=[NSMutableString stringWithFormat:@"%@",@"p1 child" ];

    p1.c=str;                       //将直接指向str,所以他的地址相同与strassign)

    p1.b=str;                      //直接复制一个新的存储到里面,地址与str不相同(copy)

    int count=[p1.c retainCount];

    int count2=[p1.b retainCount];

    NSLog(@"%d",count);

    NSLog(@"%d",count2);


 // Override point for customization after application launch.

    return YES;

}


第二天


#import "AppDelegate.h"


@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

   

   

    NSString *a=[NSStringstringWithFormat:(@"This is a string")];

    NSString *b=[NSStringstringWithString:a];

   NSLog(@"%@",a);

   NSLog(@"%@",b);  //用两种方法构建字符串

 


   int c=[alength];

   NSLog(@"%d",c);//确定字符串的长度


   char d;

    d=[a characterAtIndex:3];

   NSLog(@"%c",d);//取出字符串中第三个字符



    NSString *str;

    str=[a stringByAppendingFormat:a];

   NSLog(@"%@",str);



    str=@"abcdefghigkmln";

   NSString *str2=[strsubstringToIndex:(10)];

   NSLog(@"%@",str2);//取出字符串中的前十个字符



    str2=[strsubstringFromIndex:(11)];

   NSLog(@"%@",str2);//去掉字符串中的前十个字符,输出后十个字符



    str=[struppercaseString];

   NSLog(@"%@",str);//将字符全部改成大写



    str=[strlowercaseString];

   NSLog(@"%@",str);//将字符全部改成小写



   if([strisEqualToString:str2])

    {

       NSLog(@"相等");

    }

   else

       NSLog(@"不行等");//判断字符串是否相等


   if([strcompare:(str2)])

    {

       NSLog(@"相等");

    }

       else

           NSLog(@"不行等");//用compare比较字符串是否相等


    NSString *s=[NSStringstringWithFormat:(@"1234.567")];

   NSLog(@"%d",[sintValue]);//转化int型

   NSLog(@"%f",[sfloatValue]);//转化浮点型

   NSLog(@"%d",[sboolValue]);//转化布尔型

   NSLog(@"%.2f",[sdoubleValue]);//转化双精度

    NSString *str3=[NSStringstringWithFormat:(@"My story is the type of")];

   NSArray *shu=[str3componentsSeparatedByString:@" "];//将字符串按照“ ”截取

   NSLog(@"%@",shu);

    

    


 

 // Override point for customization after application launch.

    returnYES;

}



2013-07-25 20:47:10.537 Twoday[874:11303] This is a string

2013-07-25 20:47:10.538 Twoday[874:11303] This is a string

2013-07-25 20:47:10.538 Twoday[874:11303] 16

2013-07-25 20:47:10.539 Twoday[874:11303] s

2013-07-25 20:47:10.539 Twoday[874:11303] This is a stringThis is a string

2013-07-25 20:47:10.539 Twoday[874:11303] abcdefghig

2013-07-25 20:47:10.540 Twoday[874:11303] mln

2013-07-25 20:47:10.540 Twoday[874:11303] ABCDEFGHIGKMLN

2013-07-25 20:47:10.541 Twoday[874:11303] abcdefghigkmln

2013-07-25 20:47:10.541 Twoday[874:11303]不行等

2013-07-25 20:47:10.542 Twoday[874:11303]相等

2013-07-25 20:47:10.542 Twoday[874:11303] 1234

2013-07-25 20:47:10.542 Twoday[874:11303] 1234.567017

2013-07-25 20:47:10.543 Twoday[874:11303] 1

2013-07-25 20:47:10.543 Twoday[874:11303] 1234.57

2013-07-25 20:47:10.544 Twoday[874:11303] (

    My,

    story,

    is,

    the,

    type,

    of

)



原创粉丝点击