objective-c的常用基础类

本文通过示例介绍了Objective-C中几个常用基础类的使用方法,包括NSArray、NSDictionary、NSString及其可变版本的初始化、操作及遍历等基本功能。

几个常用的基础类的使用示例

只弄了几个 下次去实验室再继续用iMac测试

//
//  main.m
//  Array_Test
//
//  Created by mac11 on 12-3-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        NSArray *array = [ [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six", nil];
        int count = [array count];
        NSLog(@"This array has %d elements.\n",count);
        
        //判断元素是否在数组中
        if([array containsObject:@"Two"])
            NSLog(@"Two is in the array");
        else
            NSLog(@"It's not in the array");
       
        //返回索引最大的那个
        id object = [array lastObject];
        NSLog(@"%@",object);
        
        object = [array objectAtIndex:3];
        NSLog(@"%@",object);
        
        NSMutableArray *marray = [[NSMutableArray alloc] init];
        //复制整个数组
        marray = [NSMutableArray arrayWithArray:array];
        NSLog(@"NSMUlableArray :%@",marray);
       
        /*
        NSEnumerator *enumerator = [anArray objectEnumerator];
        id object;
        
        while ((object = [enumerator nextObject])) {
            // do something with object...
        */
        NSEnumerator *enumerator = [array objectEnumerator];
        id object1;
        int num=1;
        while(object1 = [enumerator nextObject])
        {
            NSLog(@"The %ith element of the array is :%@",num++,object1);
        }
        
        
    }
    return 0;
}

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
       //NSLog(@"Hello, World!");
        NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"Value1",@"key1",@"value2",@"key2", nil];
        int count = [dic count];
        NSLog(@"The Dictionary is :%@",dic);
        NSLog(@"The count is %i",count);
        
    }
    return 0;
}

//
//  main.m
//  string_test
//
//  Created by mac11 on 12-3-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        NSLog(@"Hello, World!");
        NSString *str1 = @"I'm a men,this is a NSstring";
        NSLog(@"%@",str1);
        NSString *str2 = [[NSString alloc] initWithString:@"This is the string too"];
        NSLog(@"%@",str2);
        NSString *str3 = [[NSString alloc] initWithFormat:@"My height is %i,and my weight is %i.\n",170,65];
        NSLog(@"%@",str3);
        
        NSString *str4 = @"This is";
        if([str2 hasPrefix:str4])
            NSLog(@"Oh yes");
        else
            NSLog(@"oh no");
        if([str1 isEqualToString:str2])
            NSLog(@"We are the same");
        else
            NSLog(@"We are different");
        NSUInteger length=[str4 length];
        NSLog(@"The length of the string is %i",length);
        
        NSMutableString *mstr1 = [[NSMutableString alloc] initWithCapacity:42];
        [mstr1 appendString:@"I'm the MutableString ~"];
        [mstr1 appendFormat:@", oh no~"];
        NSLog(@"%@",mstr1);
        
        [mstr1 insertString:@"lallala~~ " atIndex:4];
        NSLog(@"%@",mstr1);
        
        //更多的功能用 alt+NSString了解
    }
    return 0;
}

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // NSString usage
	NSString *str0 = [NSString stringWithString:@"Hello"]; // created by class method,don't need to be released by creator
	NSString *str = [[NSString alloc]initWithString:@"Hello,world"];
	NSLog(@"%@",str);
	if([str isEqualToString:@"Hello,world"]==YES){
		NSLog(@"str is equal to Hello,world");
	}
	// seperate the string by ,
	NSArray *subStr = [str componentsSeparatedByString:@","];
	[str release];	
	
	// NSNumber usage
	NSNumber *num = [NSNumber numberWithInt:30];
	NSNumber *myNum=[[NSNumber alloc]initWithBool:YES];
	if([[myNum className] isEqualToString:@"NSCFNumber"]){
		NSLog(@"a numeric value\n");
	}
	else if([[myNum className] isEqualToString:@"NSCFBoolean"]){
		NSLog(@"a boolean value\n");
	}
	NSLog(@"number=%@\n",myNum);
	[myNum release];
	
	// NSArray usage
	// create an array containing contents of the specified file. the file must be of type .plist
	//NSArray *myArr2 = [[NSArray alloc] initWithContentsOfFile:@"mydata.plist"];
	// create an array from objects(can be different types!),the last one must be nil
	NSArray *myArr = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",nil];
	NSString *myStr = [myArr objectAtIndex:2];  // [myArr lastObject]  [myArr count]
	NSLog(@"%@",myStr);
	// looping through the array
	for(id obj in myArr)
		NSLog(@"array element=%@\n",obj);
	[myArr release];
	
	// NSMutableArray usage
	NSMutableArray *myMutArr = [[NSMutableArray alloc]initWithObjects:@"One",@"Two",@"Three",nil];
	[myMutArr addObject:@"Four"];  // [myMutArr addObject:myNum] OK
	[myMutArr insertObject:@"Extra" atIndex:1];
	NSLog(@"%@",[myMutArr lastObject]);
	[myMutArr release];
	
    [pool drain];
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值