一些常见又好用的代码

部分一:关于改变label

Returns a rectangle that is smaller or larger than the source rectangle, with the same center point.


    CGContextRef context=UIGraphicsGetCurrentContext();

    //draws a rectangle

    

    CGContextStrokeRect(context, CGRectInset(self.bounds, 1.0, 1.0));

    

   [super drawTextInRect: CGRectInset(rect, 5, 5)];

//通过以上得代码,画个框,在一个更内部得范围内画了字符。。。。

部分二 

#import <Foundation/Foundation.h>


@interface NSString (StringCategories) 

-(NSString*) basePictureName;

@end



产生一个类,然后把类名搞在括号里,interface 名称改为要extend 的 类

#import "StringCategories.h"


@implementation NSString (StringCategories)

-(NSString*) basePictureName{

    return [self stringByAppendingString:@"IO"];

}

@end

//通过以上的方式,也可以分开同一个类在不同的文件中实现


部分3

协议,我们最常用的就是在interface中<protocolName>, 也可以自定义protocol.

@protocol ProtocolName <Class>

-(void) methodName1...

@optional

....

@end


还有一个用法就是作为范型的一个检查:

@property (non atomic,assign) id<protocol> propertyName

从这个定义来看,这个属性没有特定的类型要求,来自id,但要求是符合protocol。。。 。。


部分四 optional method

我们看到optional method,一般用在两个地方: class category 和custom protocol,但需要一个机制来检查特定对象有没有实现optional的方法:

myclass * mc=[[myclass alloc] init];

if ([mc respondstoSelector:@selector(woohoo)])// 这样就返回了是否支持的结果

如果支持就调用[mc woohoo]

//


-(BOOL) respondsToSelector:(SEL)aSelector

{

    NSLog(@"%@",NSStringFromSelector(aSelector));

    return [super respondsToSelector:aSelector];

}

在application delegate中,其实很多方式多进行了判断,以下就是一些顺序。

applicationWillResignActive:

2012-04-08 11:16:42.872 TestUIEffect[707:f803] applicationDidEnterBackground:

2012-04-08 11:16:42.919 TestUIEffect[707:f803] applicationWillEnterForeground:

2012-04-08 11:16:42.919 TestUIEffect[707:f803] applicationWillSuspend:

2012-04-08 11:16:42.920 TestUIEffect[707:f803] application:didResumeWithOptions:

2012-04-08 11:16:42.962 TestUIEffect[707:f803] application:didFinishLaunchingWithOptions:


部分五 :

NSRange/NSNOtfound/NSregularexpression/NSTextCheckingResult/NSMutalbeString/

  //NSMakeRange(<#NSUInteger loc#>, <#NSUInteger len#>)

    //NSRangeFromString(<#NSString *aString#>)


部分六:

[NSumber numberWithInt:5] //初始化

[NSUserDefaults standardUserDefaults] 标准应用本地用户存储

  [[NSUserDefaults standardUserDefaultsregisterDefaults:data] 存入数据



  NSDictionary* data=[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:4],@"cardRows",[NSNumber numberWithInt:3],@"cardColums", nil];

    [[NSUserDefaults standardUserDefaults] registerDefaults:data];


备注:由于nsinteger 本身不是数字,不能直接算术运算,可以通过 intValue来获取,


    NSUserDefaults* ud=[NSUserDefaults standardUserDefaults];

    int therows=[ud integerForKey:@"cardRows"];

    int thecolumns=[[ud objectForKey:@"cardColumns"] intValue];


部分七:

NSData 是用于bytes序列的,一般会在网络访问中用到,还有就是序列化需要的时候,比如把 UIColor转化为 NSData存储到 NSUserDefaults

Archives and serializations are two ways in which you can create architecture-independent byte streams of hierarchical data. Byte streams can then be written to a file or transmitted to another process, perhaps over a network. When the byte stream is decoded, the hierarchy is regenerated. Archives provide a detailed record of a collection of interrelated objects and values. Serializations record only the simple hierarchy of property-list values.

   NSDictionary* colorDict=[NSDictionary dictionaryWithObjectsAndKeys:[NSKeyedArchiver archivedDataWithRootObject:[UIColor blueColor]],@"mycolor",nil];

    [[NSUserDefaults standardUserDefaults] registerDefaults:colorDict];

You should read this document to learn how to create and extract archived representations of object graphs.

   NSDictionary* colorDict=[NSDictionary dictionaryWithObjectsAndKeys:[NSKeyedArchiver archivedDataWithRootObject:[UIColor blueColor]],@"mycolor",nil];

    [[NSUserDefaults standardUserDefaults] registerDefaults:colorDict];



  1. NSString *str = @"abc";  
  2. NSString *astr = @"efg";  
  3. NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];  
  4.        
  5. //保存数据  
  6. NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filename = [Path stringByAppendingPathComponent:@"test.plist"];  
  7. [NSKeyedArchiver archiveRootObject:Array toFile:filename];  
  8.        
  9. str = @"a";  
  10. astr = @"";  
  11.        
  12. //加载数据  
  13. NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];  
  14. str   =  [arr objectAtIndex:0];  
  15. astr =  [arr objectAtIndex:1];  
  16.        
  17. NSLog(@"str:%@",str);  
  18. NSLog(@"astr:%@",astr);  





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值