您的位置:首页 > 编程语言

一些常见又好用的代码

2012-04-08 13:08 267 查看
部分一:关于改变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 standardUserDefaults] registerDefaults: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];

NSString *str = @"abc";
NSString *astr = @"efg";
NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];

//保存数据
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filename = [Path stringByAppendingPathComponent:@"test.plist"];
[NSKeyedArchiver archiveRootObject:Array toFile:filename];

str = @"a";
astr = @"";

//加载数据
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];
str = [arr objectAtIndex:0];
astr = [arr objectAtIndex:1];

NSLog(@"str:%@",str);
NSLog(@"astr:%@",astr);

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: