Foundation中各种初始化创建

NSString


初始化字符串一 

//经典字符串初始化
        NSString *q1=@"      暗号";//这样初始化,系统自动帮我们管理释放内存
        NSLog(@"%@",q1);

初始化字符串二 

//(重点)格式化字符串
        NSString *a=@"虎",*b=@"妖";
        NSString *q2=[NSString stringWithFormat:@"天网盖地%@,宝塔镇河%@",a,b];
        NSLog(@"%@",q2);

初始化字符串三

NSString *q3=[[NSString alloc]init];//需要自己来释放内存
        q3=@"    芝麻开门";
        NSLog(@"%@",q3);




NSSMutableString

//初始化一

NSMutableString *str1=[[NSMutableString alloc]initWithCapacity:10];
        //实例化的另一种方法  写一个分配字符串的大小,不可以超过这个范围
        [str1 setString:@"Hello"];//因为为可变,所以要这样赋值初始化,
        NSLog(@"%@",str1);

//初始化二

NSMutableString *str2=[[NSMutableString alloc]init];
        [str2 appendFormat:@"Hello"];
        NSLog(@"%@",str2);

NSString *c=@" world";
        [str2 appendFormat:@"%@",c];
        NSLog(@"%@",str2);



NSArray

//初始化一

NSArray *array1=[[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];//因为是不可变的数组,实例化时必须对它赋值  nil(niu)为结束.
        NSLog(@"%@",array1);


//初始化二

SArray *array2 = @[@"abc",@"def"];
        NSLog(@"%@",array2);

//初始化三:通过c数组初始化NSArray

NSString *str3[3];
        str3[0] = @"11";
        str3[1] = @"22";
        str3[2] = @"33";
        NSArray *array3 = [NSArray arrayWithObjects:str3 count:3];
        NSLog(@"%@",array3);


NSMutableArray

//初始化一

NSMutableArray *array4 = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3", nil];
        NSLog(@"%@",array4);

//初始化二

(先写一个类,初始化定义)


Ps.h

#import <Foundation/Foundation.h>

@interface Ps : NSObject

@property(nonatomic,strong)NSString *Name;
- (instancetype)initWithName:(NSString *)Name;
@end


Ps.m

#import "Ps.h"

@implementation Ps
- (instancetype)initWithName:(NSString *)Name//初始化
{
    self = [super init];
    if (self) {
        _Name=Name;
    }
    return self;
}
- (NSString *)description//输出显示
{
    return [NSString stringWithFormat:@"%@", _Name];
}
@end


开始初始化

Ps *p1=[[Ps alloc]initWithName:@"zs"];
        Ps *p2=[[Ps alloc]initWithName:@"ls"];
        Ps *p3=[[Ps alloc]initWithName:@"ww"];
        
        NSMutableArray *array5=[[NSMutableArray alloc]initWithObjects:p1,p2,p3, nil];
        NSLog(@"%@",array5);


//初始化三

NSMutableArray *array6=[[NSMutableArray alloc]init];
        [array6 addObject:p1];//添加单个元素
        NSLog(@"%@",array6);
        [array6 addObjectsFromArray:array5];//从数组中所有元素添加
        NSLog(@"%@",array6);


NSDictionary(key value键值对的形式存储(三种创建方法))


//初始化一

NSDictionary *dic1=[NSDictionary dictionaryWithObject:@"1" forKey:@"a"];
        NSLog(@"%@",dic1);

//初始化二(可以添加多组键值对)

NSDictionary *dic2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"1",@"a",@"2",@"b", nil];
        NSLog(@"%@",dic2);
        
        NSDictionary *dic3=[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1",@"2",@"3", nil] forKeys:[NSArray arrayWithObjects:@"a",@"b",@"c", nil]];
        NSLog(@"dict2=%@",dic3);

//初始化三:新版本简便写法

NSDictionary *dic4=@{@"a":@"1",@"b":@"2"};//前面为key,和前面相反
        NSLog(@"%@",dic4);

NSMutableDictionary


// 初始化一

NSMutableDictionary *dic5=[[NSMutableDictionary alloc]init];
        [dic5 setObject:@"1" forKey:@"a"];
        [dic5 setObject:@"2" forKey:@"b"];
        NSLog(@"%@",dic5)


// 初始化二

NSMutableDictionary *dic6= [NSMutableDictionary dictionaryWithObject:@"value3" forKey:@"key3"];
        NSLog(@"%@",dic6);


//初始化三

NSMutableDictionary *dic7= [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"value",@"key",@"value2",@"key2", nil];
        NSLog(@"%@",dic7);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值