随笔分类 -  Objective-C

Objective-C 2.0
摘要:重装IDP相关证书的教程,请参看如下:iphone 证书重装 全过程The identity 'iPhone Developer' doesn't match any valid certificate - neil's notebook这里只提一些关键点:1、先安装WWDR证书。在Certificates标签下可下载安装。*If you do not have the WWDR intermediate certificate installed, click here to download now.注意:需要安装在“钥匙串访问”的“登录”下。2、生成公私密 阅读全文
posted @ 2011-07-05 11:28 chenjunbiao 阅读(830) 评论(0) 推荐(0)
摘要:通过分类的方式可以为已存在的类添加新的方法,甚至不需要源码,有点像C#中的扩展方法。这时提供一个例子是把一个字符串转换为驼峰式并且出掉单词空格。NSString+CamelCase.h#import <Foundation/Foundation.h>//NSString 表示将要添加分类的类名称,该类必须是已存在的。//CamelCase 是为类添加的分类的名称。//只能添加方法,不能添加变量。//头文件命名惯例:ClassName+CategoryName.h@interface NSString (CamelCase)-(NSString*) camelCaseString;@ 阅读全文
posted @ 2011-05-12 10:17 chenjunbiao 阅读(1165) 评论(0) 推荐(0)
摘要:实现了NSFastEnumeration协议的集合的都可以使用快速枚举的特性,如NSArray, NSDictionary, NSSet, NSEnumerator等。注意这里NSEnumerator也实现了NSFastEnumeration协议,这样可以执行一些特殊的任务,如使用reverseObjectEnumerator来对数组进行反同查询。快速枚举的格式如下:for(type loopVariable in expression){ 语句}下面的例子关注一下不同退出循环的方式,loopVariable值的变化:#import <Foundation/Foundation.h> 阅读全文
posted @ 2011-05-11 10:20 chenjunbiao 阅读(903) 评论(0) 推荐(0)
摘要:集合类(如:NSArray、NSSet、NSDictionary等)均可获取到NSEnumerator, 该类是一个抽象类,没有用来创建实例的公有接口。NSEnumerator的nextObject方法可以遍历每个集合元素,结束返回nil,通过与while结合使用可遍历集合中所有项。例子中使用了与前例相同的Photo对象,具体定义参考隐式循环这一节。#import <Foundation/Foundation.h>#import "Photo.h"int main (int argc, const char * argv[]){ NSAutoreleasePoo 阅读全文
posted @ 2011-05-11 09:40 chenjunbiao 阅读(3563) 评论(1) 推荐(1)
摘要:定义一个Photo类,并带有2个draw方法, 一个带参数、一个不带参数。Photo.h#import <Foundation/Foundation.h>@interface Photo : NSObject {@private }-(void) draw;-(void) draw:(NSNumber*) number;@endPhoto.m#import "Photo.h"@implementation Photo- (id)init{ self = [super init]; if (self) { // Initialization code here. 阅读全文
posted @ 2011-05-10 10:37 chenjunbiao 阅读(910) 评论(0) 推荐(0)
摘要:代码:#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; //Objective-C 没有提供相关的函数生成随机数,不过C供了rand(), srand(), random(), srandom(), arc4random()几个函数。 //其中arc4random()不用seed //生成0到100的随机数 int x = arc4random() % 100; 阅读全文
posted @ 2011-05-10 09:58 chenjunbiao 阅读(629) 评论(0) 推荐(0)
摘要:参考文章:http://blog.codingmylife.com/?p=85#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString *path = @"/Users/billchen/Desktop/f1.rtf"; NSString *temp = @"Hello Friend"; int i = 100; 阅读全文
posted @ 2011-05-09 13:14 chenjunbiao 阅读(2681) 评论(0) 推荐(0)
摘要:NSNumber可以包装常规的C类型成为一个Objective-C对象,可以存储任何的数字类型,以及BOOL和char。#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSMutableArray *array = [NSMutableArray arrayWithCapacity:3]; [array addObject:[NSNumber numberWit 阅读全文
posted @ 2011-05-06 10:10 chenjunbiao 阅读(2529) 评论(0) 推荐(0)
摘要:NSMutableDictionary 用于处理值对集合。保存到键的对象必须实现了copyWithZone:方法。和NSMutableArray一样添加到容器时对象收到retain消息,把对象从容器中移除时收到release消息,任何想在移除对象还要对该对象进行操作必须先对该对象发出一条retain消息,在使用完成后需要release。#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){ NSAutoreleasePool * pool = [[NSAutoreleasePool all 阅读全文
posted @ 2011-05-06 09:53 chenjunbiao 阅读(3279) 评论(1) 推荐(0)
摘要:NSMutableArray 只能保存Objective-C对象,没有边界检查,超出边界会抛出运行时异常。添加到Array中的对象会收到一条retain消息,当从数组中删除该对象或者数组本身被release了数组中的对象都会收到一条release消息。#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSMutableArray *mutableFruitBask 阅读全文
posted @ 2011-05-06 09:32 chenjunbiao 阅读(592) 评论(0) 推荐(0)
摘要:1. 折分字符串操作:#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; //将一个句子分解为单独的单词 NSString *string1 = @"my name is bill"; NSArray *words = [string1 componentsSeparatedByCharactersInSet:[NSCharacterSet wh 阅读全文
posted @ 2011-05-04 10:12 chenjunbiao 阅读(817) 评论(0) 推荐(0)
摘要:在Objective-C中是没有静态类变量一说的,不过我们可以模拟出来,但如果类对象之间还有继承关系,这样就一点复杂的了。我们这里假设一个Coupon类对象需要保存当前还剩多少有优惠券,还有一个继承于Coupon类对象的NewCoupon类对象,但它也保存有自已可用的优惠券的数量,我们如何保证他们的静态类变量相互独立,请看下面的例子。Coupon.h#import <Foundation/Foundation.h>@interface Coupon : NSObject { }+(int) numberCouponsLeft;+(void) resetCoupon;@endCoup 阅读全文
posted @ 2011-05-03 10:24 chenjunbiao 阅读(981) 评论(0) 推荐(0)
摘要:#如果你想在运行时通过给定的字符串来动态创建对应的对象并且调用某中的方法。#如果你得到一个id对象,但不知道该对象是否含有某方法,如果有就调用它。请看下面的例子:DynObj.h#import <Foundation/Foundation.h>@interface DynObj : NSObject { NSString *name;}@property(nonatomic,retain) NSString* name;- (id) init;- (void) dealloc;-(void) show;@endDynObj.m#import "DynObj.h" 阅读全文
posted @ 2011-04-29 09:43 chenjunbiao 阅读(341) 评论(0) 推荐(0)
摘要:Tester.h#import <Foundation/Foundation.h>@interface Tester : NSObject { }-(void) test:(NSString*) msg;-(void) notImp;@endTester.m#import "Tester.h"@implementation Tester-(void) test:(NSString*) msg{ NSLog(@"%@", msg);}@end注意:没有实现notImp方法main.m#import <Foundation/Foundati 阅读全文
posted @ 2011-04-21 09:50 chenjunbiao 阅读(12147) 评论(2) 推荐(0)
摘要:Tester.h#import <Foundation/Foundation.h>@interface Tester : NSObject { }-(void) Test:(NSString*) msg;@endTester.m#import "Tester.h"@implementation Tester-(void) Test:(NSString*) msg{ NSLog(@"%@", msg); }@endmain.m#import <Foundation/Foundation.h>#import "Tester. 阅读全文
posted @ 2011-04-21 09:37 chenjunbiao 阅读(513) 评论(0) 推荐(0)
摘要:CurrentDate.h#import <Foundation/Foundation.h>@interface CurrentDate : NSObject { }- (NSString *) stringForDate: (NSDate *)date usingFormatter: (NSDateFormatter *)formatter;@endCurrentDate.m#import "CurrentDate.h"@implementation CurrentDate- (NSString *) stringForDate: (NSDate *)date 阅读全文
posted @ 2011-04-20 13:36 chenjunbiao 阅读(2416) 评论(0) 推荐(0)