一些常见又好用的代码
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];
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); |
相关文章推荐
- 一些常见的问题与解决代码!(五)(JS)
- 一些页面常见的问题与解决代码
- 一些常见的JS表单验证代码
- Xcode Analyze进行代码静态检查以及一些常见问题释疑
- js一些常见的问题与解决代码(转自www.51js.com)
- ASP.NET高级程序员进阶之路——.net项目开发中一些常见的代码规范问题及解决方案
- svn(subversion)代码版本管理在linux下的一些常见使用命令
- AS3一些不太常见的代码写法
- .NET中一些常见代码的性能问题
- js一些常见的问题与解决代码(2)(转自www.51js.com)
- 链表的一些常见笔试面试问题总结及代码
- Java代码规范和一些常见问题
- mysql Access denied for user \'root\'@\'localhost\'”解决办法总结,下面我们对常见的出现的一些错误代码进行分析并给出解决办法,有需要的朋友可参考一下。
- Java代码规范和一些常见问题
- Java代码规范和一些常见问题
- 预防恶意网页代码的一些建议 解除常见恶意修改的一些方法
- 整理android四大组件及一些常见api的通用代码
- 一些常见的问题与解决代码!(精典) 1
- AS3一些不太常见的代码写法
- 使用***版软件中常见的一些错误代码