首页 文章 精选 留言 我的

精选列表

搜索[objective-c],共1271篇文章
优秀的个人博客,低调大师

Objective-C之数字对象

int , float , long都是OC的基本数据类型,但是(!important)它们都不是对象。但是有的时候需要将他们最为一个对象来使用,例如:NSArray要求存储的值必须是对象。那么这里就可以使用NSNumber类。 一 , 为NSNumber赋值: ① :赋值一个int类型的值,创建和初始化int2O = [NSNumber numberWithInteger:100] 意义: 为int2O赋值整形100对象 ②:获得init2O的的值init2Get = [init2O integerValue] 注意 : integerValue说明init2O里面存的是int类型的值 例如: #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSNumber *init2O; NSInteger init2Get; init2O = [NSNumber numberWithInteger:100]; init2Get = [init2O integerValue]; NSLog(@"%li",(long)init2Get); } return 0; } 结果: 二,可以使用initWithInteger来直接实例化一个NSNumber 其他的类型 赋值方法 实例化 检索方法 numberWithChar initWithChar charValue numberWithUnsignedChar initWithUnsignedChar unsignedCharValue numberWithShort initWithShort shortValue numberWithUnsignedShort initWithUnsignedShort unsignedShortValue numberWithInteger initWithInteger integerValue numberWithUnsignedInteger initWithUnsignedInteger unsignedIntegerValue numberWithInt initWithInt intValue numberWithUnsignedInt initWithUnsignedInt unsignedIntValue numberWithLong initWithLong longValue numberWithUnsignedLong initWithUnsignedLong unsignedLongValue numberWithLongLong initWithLongLong longlongValue numberWithUnsignedLongLong initWithUnsignedLongLong unsignedLongLongValue numberWithFloat initWithFloat floatValue numberWithDouble initWithDouble doubletValue numberWithBool initWithBool booltValue 验证2个number是否是相等的 #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSNumber *init2O = [[NSNumber alloc] initWithInteger:100]; NSInteger init2Get; init2Get = [init2O integerValue]; NSLog(@"%li",(long)init2Get); //验证是否相等 NSNumber *float2O = [[NSNumber alloc] initWithFloat:100.00]; if( [init2O isEqualToNumber:float2O] == YES){ NSLog(@"Equal!!!"); } } return 0; } 结果: 验证小于 #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSNumber *init2O = [[NSNumber alloc] initWithInteger:100]; NSInteger init2Get; init2Get = [init2O integerValue]; NSLog(@"%li",(long)init2Get); //验证是否相等 NSNumber *float2O = [[NSNumber alloc] initWithFloat:100.00]; if( [init2O compare:float2O] == NSOrderedAscending){ NSLog(@"Asc!!!"); }else{ NSLog(@"No Asc"); } } return 0; } 结果: 本文转自Aonaufly51CTO博客,原文链接:http://blog.51cto.com/aonaufly/2054108,如需转载请自行联系原作者

优秀的个人博客,低调大师

Objective-c中的分类

使用Object-C中的分类,是一种编译时的手段,允许我们通过给一个类添加方法来扩充它(但是通过category不能添加新的实例变量),并且我们不需要访问类中的代码就可以做到,这点和javascript中使用原型来定义属性有点类似。 我们可以为一个类创建一个新的方法,而不需要在代码中编辑类定义。 下面就是定义并使用分类的例子程序,通过下面代码,我们可以给Object-C中的NSString 添加camelCaseString分类,使用camelCaseString方法,就可以去掉一个字符串中的空格,并将原有空格后的单词改写成大写(即将字符串转化为驼峰式)。 [cpp] view plain copy #import<Foundation/Foundation.h> /* 定义分类的过程大致可分为以下几个步骤: 第一步、创建一个带有接口的新文件,即创建已有类 第二步、在新文件中添加需要扩展的方法及方法的实现,即需要添加的分类 */ //NSString表示将要添加分类的类名称,该类必须是已存在的。 //CamelCase是为类添加的方法名称。 //只能添加方法,不能添加变量。 //头文件命名惯例:ClassName+CategoryName.h @interfaceNSString(CamelCase) -(NSString*)camelCaseString; @end @implementationNSString(CamelCase) -(NSString*)camelCaseString { //调用NSString的内部方法获取驼峰字符串。 //self指向被添加分类的类。 NSString*castr=[selfcapitalizedString]; //创建数组来过滤掉空格,通过分隔符对字符进行组合。 NSArray*array=[castrcomponentsSeparatedByCharactersInSet: [NSCharacterSetwhitespaceCharacterSet]]; //把数组的字符输出 NSString*output=@""; for(NSString*wordinarray) { output=[outputstringByAppendingString:word]; } returnoutput; } @end intmain(intargc,constchar*argv[]) { NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init]; NSString*str=@"Mynameisbill."; NSLog(@"%@",str); str=[strcamelCaseString]; NSLog(@"%@",str); [pooldrain]; return0; } 本文转自 驿落黄昏 51CTO博客,原文链接:http://blog.51cto.com/yiluohuanghun/1153613,如需转载请自行联系原作者

优秀的个人博客,低调大师

Objective-C:分类(Category、extension)

分类(Category 、Extension) (一)分类的划分 (2) 1、(命名的类别)类别 Category:只能添加新的方法,不能添加新变量。 2、(未命名的类别)类的扩展 Extension://扩展一般直接写在类的实现文件中 //扩展中定义的都是类中私有的实例变量和方法 (二)分类的使用 (1)分类只能增加方法(包括类方法和对象方法),不能增加成员变量 (2)在分类方法的实现中可以访问原来类中的成员变量; (3)分类中可以重新实现原来类中的方法,但是会覆盖掉原来的方法,导致原来的方法无法再使用(警告); (4)方法调用的优先级:分类->原来的类->父类,若包含有多个分类,则最后参与编译的分类优先; (5)在很多的情况下,往往是给系统自带的类添加分类,如NSObject和NSString,因为有的时候,系统类可能并不能满足我们的要求。 (6)在大规模的应用中,通常把相应的功能写成一个分类,可以有无限个分类,对原有类进行扩充,一般分模块写,一个模块一个分类。 //自定义的字符串类String 1 // String.h 2 // 类别 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 10 @interface String : NSObject 11 @property(nonatomic,strong)NSString* string; 12 -(id)initWithString:(NSString*) str; 13 -(void)print; 14 @end 1 // String.m 2 // 类别 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import "String.h" 9 //#import "String_String_private.h"//在String类单独导入一个类扩展文件 10 11 //类扩展extension,添加的属性str1和show方法都是私有的,只能在String类中可以访问得到 12 @interface String()//直接将类扩展文件写入String类的实现中 13 { 14 NSString *str1; 15 } 16 -(void)show; 17 @end 18 19 20 @implementation String 21 -(id)initWithString:(NSString*) str 22 { 23 self = [super init]; 24 if(self) 25 { 26 _string = str; 27 } 28 return self; 29 } 30 -(void)show 31 { 32 NSLog(@"string is runing"); 33 } 34 -(void)print//通过print方法访问到类扩展中添加的属性和方法 35 { 36 [self show]; 37 NSLog(@"%@",_string); 38 } 39 @end //单独的类扩展文件 1 // String_String_private.h 2 // 类别 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import "String.h" 9 10 @interface String () 11 { 12 NSString *str1; 13 } 14 //@property(nonatomic,strong)NSString* str1; 15 -(void)show; 16 @end //类别文件 1 // String+Count.h 2 // 类别 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import "String.h" 9 //String的类别Count,添加的方法,对String类进行功能的扩展(切记:类别中只能添加方法) 10 @interface String (Count) 11 -(NSInteger) nmuberOfString: (NSString *)str; 12 -(NSInteger) numberOfCount: (NSString *)str; 13 @end 1 // String+Count.m 2 // 类别 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import "String+Count.h" 9 10 @implementation String (Count) 11 -(NSInteger) nmuberOfString: (NSString *)str 12 { 13 NSInteger count = 0; 14 for(int i=0;i<str.length;i++) 15 { 16 unichar ch = [str characterAtIndex:i]; 17 if(ch>='0' && ch<='9') 18 count++; 19 } 20 return count; 21 } 22 -(NSInteger) numberOfCount: (NSString *)str 23 { 24 NSInteger count = 0; 25 for(int i=0;i<str.length;i++) 26 { 27 unichar ch = [str characterAtIndex:i]; 28 if(ch>='0' && ch<='9') 29 count++; 30 } 31 return count; 32 } 33 @end //主函数测试 1 // main.m 2 // 类别 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 #import "String+Count.h" 10 #import "String.h" 11 int main(int argc, const char * argv[]) 12 { 13 @autoreleasepool 14 { 15 //测试类扩展(未命名的类别) 关键字:extension 16 String *string = [[String alloc]initWithString:@"xiayuan"]; 17 [string print]; 18 19 20 //测试类别(命名的类别) 关键字:Category 21 NSInteger a = [string nmuberOfString:@"xu3984jjsn6ew32"]; 22 NSLog(@"a = %ld",a); 23 24 NSInteger b = [string numberOfCount:@"36y3jfse93u4gdf8"]; 25 NSLog(@"b = %ld",b); 26 } 27 return 0; 28 } //运行结果 2015-08-12 15:24:06.281类别[1304:75695] string is runing 2015-08-12 15:24:06.282类别[1304:75695] xiayuan 2015-08-12 15:24:06.282类别[1304:75695] a = 7 2015-08-12 15:24:06.282类别[1304:75695] b = 7 Program ended with exit code: 0 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4724612.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

Objective-C:浅复制(拷贝)

浅复制:复制对象时,如果对象中包含对象类型的实例变量,只是复制指针。新对象中的对象类型实例变量和旧对象中的对象类型实例变量指的是同一个对象。任何一方实例变量对对象做修改,另一方实例变量指向的该对象也就改变了。 .h声明文件 1 // Person.h 2 // 03-对象复制 3 // 4 // Created by ma c on 15/8/14. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 10 @interface Person : NSObject<NSCopying>//实现NSCopying协议才能支持自定义对象的复制11 @property(nonatomic,retain)NSMutableString *name;//(retain)浅拷贝,拷贝对象的引用,其实用的还是一个对象,相当于一个人给自己起了一个别名 12 @property(nonatomic,assign)NSInteger age; 13 -(id)initWithName:(NSMutableString*)name andAge:(NSInteger)age; 14 -(void)show; 15 @end .m实现文件 1 // Person.m 2 // 03-对象复制 3 // 4 // Created by ma c on 15/8/14. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import "Person.h" 9 10 @implementation Person 11 -(id)initWithName:(NSMutableString*)name andAge:(NSInteger)age 12 { 13 self = [super init]; 14 if(self) 15 { 16 _name = [name retain];//强引用,获得对象所有权 17 _age = age; 18 } 19 return self; 20 } 21 -(void)show 22 { 23 NSLog(@"name:%@,age:%lu",_name,_age); 24 } 25 -(void)dealloc 26 { 27 [_name release]; 28 [super dealloc]; 29 } 30 -(id)copyWithZone:(NSZone *)zone 31 { 32 return [[Person alloc]initWithName:_name andAge:_age]; 33 } 34 @end main.m主函数 1 // main.m 2 // 03-对象复制 3 // 4 // Created by ma c on 15/8/14. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 #import "Person.h" 10 int main(int argc, const char * argv[]) { 11 @autoreleasepool { 12 // 测试Person类 13 NSMutableString *name = [NSMutableString stringWithString:@"Tom"]; 14 15 Person *p1 = [[Person alloc]initWithName:name andAge:20]; 16 [p1 show]; 17 18 [name appendString:@"Jerry"]; 19 20 Person *p2 = [[Person alloc]initWithName:name andAge:21]; 21 [p2 show]; 22 23 [p1 show]; 24 25 //对象复制,自定义对象必须要实现NSCopying协议才能支持对象复制 26 Person *p3 = [p1 copy]; 27 [p3 show]; 28 29 [p1.name appendString:@"123"];//因为retain,p1和p2指向同一对象,一改都改 30 [p3 show]; 31 32 NSMutableString *name2 = [NSMutableString stringWithString:@"Jobs"]; 33 [p1 setName:name2];//p1和p3指向的是不同的对象,互不影响 34 [p3 show]; 35 36 [p1 release]; 37 [p2 release]; 38 [p3 release]; 39 } 40 return 0; 41 } 测试结果如下: 2015-08-14 16:40:59.293 03-对象复制[1460:96685] name:Tom,age:20 2015-08-14 16:40:59.294 03-对象复制[1460:96685] name:TomJerry,age:21 2015-08-14 16:40:59.294 03-对象复制[1460:96685] name:TomJerry,age:20 2015-08-14 16:40:59.295 03-对象复制[1460:96685] name:TomJerry,age:20 2015-08-14 16:40:59.295 03-对象复制[1460:96685] name:TomJerry123,age:20 2015-08-14 16:40:59.295 03-对象复制[1460:96685] name:TomJerry123,age:20 Program ended with exit code: 0 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4730496.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

Objective-C:深复制(拷贝)

深复制:复制对象时,如果对象中包含对象类型的实例变量,要对对象类型的实例变量也要做对象复制。新对象中的对象类型实例变量和旧对象中的对象类型实例变量指的是不同的对象。不管任何一方实例变量对对象做修改,都互相不影响对方所指向的对象的内容。 .h文件 1 // Person.h 2 // 03-对象复制 3 // 4 // Created by ma c on 15/8/14. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 10 @interface Person : NSObject<NSCopying> 11 @property(nonatomic,copy)NSMutableString *name;//(copy)深拷贝 12 @property(nonatomic,assign)NSInteger age; 13 -(id)initWithName:(NSMutableString*)name andAge:(NSInteger)age; 14 -(void)show; 15 @end .m文件 // Person.m // 03-对象复制 // // Created by ma c on 15/8/14. // Copyright (c) 2015年. All rights reserved. // #import "Person.h" @implementation Person -(id)initWithName:(NSMutableString*)name andAge:(NSInteger)age { self = [super init]; if(self) { //复制对象有两种, //copy:复制出来的是一个不可变对象 //mutableCopy:复制出来的是一个可变对象 /* 对OC内置对象来说,内置类型有可变和不可变之分, 自定义的类没有这个区别,都是可变的。 */ _name = [name mutableCopy]; _age = age; } return self; } -(void)show { NSLog(@"name:%@,age:%lu",_name,_age); } -(void)dealloc { [_name release]; [super dealloc]; } -(id)copyWithZone:(NSZone *)zone { //深复制 //创建对象类型实例变量的副本 //NSMutableString *name = [_name copy]; return [[Person alloc]initWithName:_name andAge:_age]; } @end .m主函数 1 // main.m 2 // 03-对象复制 3 // 4 // Created by ma c on 15/8/14. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 #import "Person.h" 10 int main(int argc, const char * argv[]) { 11 @autoreleasepool { 12 // 测试Person类 13 NSMutableString *name = [NSMutableString stringWithString:@"Tom"]; 14 15 Person *p1 = [[Person alloc]initWithName:name andAge:20]; 16 [p1 show]; 17 18 [name appendString:@"Jerry"]; 19 20 Person *p2 = [[Person alloc]initWithName:name andAge:21]; 21 [p2 show]; 22 23 [p1 show]; 24 25 //对象复制,自定义对象必须要实现NSCopying协议才能支持对象复制 26 Person *p3 = [p1 copy]; 27 [p3 show]; 28 29 [p1.name appendString:@"123"];//因为copy,p1和p2指向不同对象,互不影响 30 [p3 show]; 31 32 NSMutableString *name2 = [NSMutableString stringWithString:@"Jobs"]; 33 [p1 setName:name2];//p1和p3指向的是不同的对象,互不影响 34 [p3 show]; 35 36 [p1 release]; 37 [p2 release]; 38 [p3 release]; 39 } 40 return 0; 41 } 测试结果如下: 2015-08-14 16:54:53.912 03-对象复制[1512:99948] name:Tom,age:20 2015-08-14 16:54:53.913 03-对象复制[1512:99948] name:TomJerry,age:21 2015-08-14 16:54:53.913 03-对象复制[1512:99948] name:Tom,age:20 2015-08-14 16:54:53.913 03-对象复制[1512:99948] name:Tom,age:20 2015-08-14 16:54:53.914 03-对象复制[1512:99948] name:Tom,age:20 2015-08-14 16:54:53.914 03-对象复制[1512:99948] name:Tom,age:20 Program ended with exit code: 0 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4730533.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

Objective-C:继承的体现

典型的继承例子:形状Shape为基类,继承它的类有:点类Point、圆类Circle、球体类Sphere、矩形类Rectangle、正方形类Square 点类Point也为基类,继承它的类有:圆类Circle、球体类Sphere、矩形类Rectangle、正方形类Square 圆类Circle也为基类,继承它的类有:球体类Sphere 矩形类Rectangle为基类,继承它的类是:正方形类Square //Shape类 .h和.m文件 1 // Shape.h 2 // 继承 3 // 4 // Created by ma c on 15/8/11. 5 // Copyright (c) 2015年. All rights reserved. 6 // 形状类 7 8 #import <Foundation/Foundation.h> 9 10 @interface Shape : NSObject 11 @property(nonatomic,assign)CGFloat length; 12 @property(nonatomic,assign)CGFloat area; 13 @property(nonatomic,assign)CGFloat volum; 14 -(void)draw; 15 -(void) Area; 16 -(void) Length; 17 -(void) Volum; 18 @end 1 // Shape.m 2 // 继承 3 // 4 // Created by ma c on 15/8/11. 5 // Copyright (c) 2015年. All rights reserved. 6 // 形状类 7 8 #import "Shape.h" 9 10 @implementation Shape 11 -(void)draw 12 { 13 NSLog(@"drawing a Shape!"); 14 } 15 -(void) Area{} 16 -(void) Length{} 17 -(void) Volum{} 18 @end //Point类 .h和.m文件 1 // Point.h 2 // 继承 3 // 4 // Created by ma c on 15/8/11. 5 // Copyright (c) 2015年. All rights reserved. 6 // 点类 7 8 #import "Shape.h" 9 10 @interface MyPoint : Shape 11 @property(nonatomic,assign) CGFloat x; 12 @property(nonatomic,assign) CGFloat y; 13 -(id)initWithX:(CGFloat)m andY:(CGFloat)n; 14 -(void)show; 15 @end 1 // Point.m 2 // 继承 3 // 4 // Created by ma c on 15/8/11. 5 // Copyright (c) 2015年. All rights reserved. 6 // 点类 7 8 #import "MyPoint.h" 9 10 @implementation MyPoint 11 -(id)initWithX:(CGFloat)m andY:(CGFloat)n 12 { 13 self = [super init]; 14 if(self) 15 { 16 _x = m; 17 _y = n; 18 } 19 return self; 20 } 21 -(void)draw 22 { 23 NSLog(@"drawing a point!"); 24 } 25 -(void)show 26 { 27 NSLog(@"x:%.1f,y:%.1f",_x,_y); 28 } 29 @end //圆类Circle .h和.m文件 1 // Circle.h 2 // 继承 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 圆类 7 8 #import "MyPoint.h" 9 10 @interface Circle : MyPoint 11 @property(nonatomic,assign)CGFloat radius; 12 -(id)initWithX:(CGFloat)m andY:(CGFloat)n andRadius:(CGFloat)r; 13 @end 1 // Circle.m 2 // 继承 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 圆类 7 8 #import "Circle.h" 9 #define PI 3.14 10 @implementation Circle 11 -(id)initWithX:(CGFloat)m andY:(CGFloat)n andRadius:(CGFloat)r 12 { 13 self = [super init]; 14 if(self) 15 { 16 super.x = m; 17 super.y = n; 18 _radius = r; 19 } 20 return self; 21 } 22 -(void) Area 23 { 24 super.area = PI*_radius*_radius; 25 } 26 -(void) Length 27 { 28 super.length = 2*PI*_radius; 29 } 30 -(void)draw 31 { 32 NSLog(@"drawing a circle!"); 33 } 34 -(void)show 35 { 36 [super show]; 37 NSLog(@"radius:%.1f,area:%.1f,Length:%.1f",_radius,super.area,super.length); 38 } 39 @end //球体类Sphere .h和.m文件 1 // Sphere.h 2 // 继承 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 球体类 7 8 #import "Circle.h" 9 10 @interface Sphere : Circle 11 @property(nonatomic,assign)CGFloat z; 12 -(id)initWithX:(CGFloat)m andY:(CGFloat)n andZ:(CGFloat)t andRadius:(CGFloat)r; 13 @end 1 // Sphere.m 2 // 继承 3 // 4 // Created by ma c on 15/8/12. 5 // Copyright (c) 2015年. All rights reserved. 6 // 球体类 7 8 #import "Sphere.h" 9 #define PI 3.14 10 @implementation Sphere 11 @synthesize z; 12 -(id)initWithX:(CGFloat)m andY:(CGFloat)n andZ:(CGFloat)t andRadius:(CGFloat)r 13 { 14 if(self = [super init]) 15 { 16 super.x = m; 17 super.y = n; 18 z = t; 19 super.radius = r; 20 } 21 return self; 22 } 23 -(void)draw 24 { 25 NSLog(@"draw a sphere!"); 26 } 27 -(void) Area 28 { 29 super.area = 4*PI*super.radius*super.radius; 30 } 31 -(void) Volum 32 { 33 super.volum = 4/3*PI*super.radius*super.radius*super.radius; 34 } 35 -(void)show 36 { 37 NSLog(@"x:%.1f,y:%.1f,z:%.1f",super.x,super.y,z); 38 NSLog(@"radius:%.1f,area:%.1f,volum:%.1f",super.radius,super.area,super.volum); 39 } 40 @end //矩形类Rectangle .h和.m文件 1 // Rectangle.h 2 // 继承 3 // 4 // Created by ma c on 15/8/11. 5 // Copyright (c) 2015年. All rights reserved. 6 // 矩形类 7 8 #import "MyPoint.h" 9 10 @interface Rectangle : MyPoint 11 12 @property(nonatomic,assign) CGFloat len; 13 @property(nonatomic,assign) CGFloat hei; 14 -(id)initWith:(CGFloat)m andY:(CGFloat)n andLen:(CGFloat) l andHei:(CGFloat)h; 15 @end 1 // Rectangle.m 2 // 继承 3 // 4 // Created by ma c on 15/8/11. 5 // Copyright (c) 2015年. All rights reserved. 6 // 矩形类 7 8 #import "Rectangle.h" 9 10 @implementation Rectangle 11 -(id)initWith:(CGFloat)m andY:(CGFloat)n andLen:(CGFloat) l andHei:(CGFloat)h 12 { 13 self = [super init]; 14 if(self) 15 { 16 self.x = m; 17 self.y = n; 18 _len = l; 19 _hei = h; 20 } 21 return self; 22 } 23 -(void) Area 24 { 25 super.area = _len*_hei; 26 } 27 -(void) Length 28 { 29 super.length = 2*(_len+_hei); 30 } 31 -(void)draw 32 { 33 NSLog(@"drawing a Rectangle!"); 34 } 35 -(void)show 36 { 37 [super show]; 38 NSLog(@"len:%.1f,hei:%.1f,area:%.1f,length:%.1f",_len,_hei,super.area,super.length); 39 } 40 @end //正方形类Square .h和.m文件 1 // Square.h 2 // 继承 3 // 4 // Created by ma c on 15/8/11. 5 // Copyright (c) 2015年. All rights reserved. 6 // 正方形类 7 8 #import "Rectangle.h" 9 10 @interface Square : Rectangle 11 @end 1 // Square.m 2 // 继承 3 // 4 // Created by ma c on 15/8/11. 5 // Copyright (c) 2015年. All rights reserved. 6 // 正方形类 7 8 #import "Square.h" 9 10 @implementation Square 11 -(void) Area 12 { 13 self.area = super.len*super.hei; 14 } 15 -(void) Length 16 { 17 self.length = 4*self.len; 18 } 19 -(void)draw 20 { 21 NSLog(@"drawing a Square!"); 22 } 23 -(void)show 24 { 25 [super show]; 26 } 27 @end //主函数测试 1 // main.m 2 // 继承 3 // 4 // Created by ma c on 15/8/11. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 #import "Shape.h" 10 #import "Square.h" 11 #import "MyPoint.h" 12 #import "Rectangle.h" 13 #import "Circle.h" 14 #import "Sphere.h" 15 int main(int argc, const char * argv[]) 16 { 17 @autoreleasepool 18 { 19 Shape *shape = [[Shape alloc]init]; 20 [shape draw]; 21 printf("\n"); 22 23 24 MyPoint *mypoint = [[MyPoint alloc]initWithX:0.0 andY:0.0]; 25 [mypoint draw]; 26 [mypoint show]; 27 printf("\n"); 28 29 30 Circle *circle = [[Circle alloc]initWithX:1.1 andY:1.1 31 andRadius:5.0]; 32 [circle Area]; 33 [circle Length]; 34 [circle draw]; 35 [circle show]; 36 printf("\n"); 37 38 39 Rectangle *rectangle = [[Rectangle alloc]initWith:2.2 andY:2.2 andLen:3 andHei:8]; 40 [rectangle Area]; 41 [rectangle Length]; 42 [rectangle draw]; 43 [rectangle show]; 44 printf("\n"); 45 46 47 Square *square = [[Square alloc]initWith:3.3 andY:3.3 andLen:4 andHei:4]; 48 [square Area]; 49 [square Length]; 50 [square draw]; 51 [square show]; 52 printf("\n"); 53 54 Sphere *sphere = [[Sphere alloc]initWithX:4.4 andY:4.4 andZ:4.4 andRadius:6.0]; 55 [sphere Area]; 56 [sphere Volum]; 57 [sphere draw]; 58 [sphere show]; 59 printf("\n"); 60 } 61 return 0; 62 } //运行结果 2015-08-12 18:44:15.931 继承[1875:121866] drawing a Shape! 2015-08-12 18:44:15.933 继承[1875:121866] drawing a point! 2015-08-12 18:44:15.933 继承[1875:121866] x:0.0,y:0.0 2015-08-12 18:44:15.933 继承[1875:121866] drawing a circle! 2015-08-12 18:44:15.933 继承[1875:121866] x:1.1,y:1.1 2015-08-12 18:44:15.933 继承[1875:121866] radius:5.0,area:78.5,Length:31.4 2015-08-12 18:44:15.934 继承[1875:121866] drawing a Rectangle! 2015-08-12 18:44:15.934 继承[1875:121866] x:2.2,y:2.2 2015-08-12 18:44:15.934 继承[1875:121866] len:3.0,hei:8.0,area:24.0,length:22.0 2015-08-12 18:44:15.934 继承[1875:121866] drawing a Square! 2015-08-12 18:44:15.934 继承[1875:121866] x:3.3,y:3.3 2015-08-12 18:44:15.934 继承[1875:121866] len:4.0,hei:4.0,area:16.0,length:16.0 2015-08-12 18:44:15.935 继承[1875:121866] draw a sphere! 2015-08-12 18:44:15.935 继承[1875:121866] x:4.4,y:4.4,z:4.4 2015-08-12 18:44:15.935 继承[1875:121866] radius:6.0,area:452.2,volum:678.2 Program ended with exit code: 0 注释:继承Shape的形状基本都具有自己的坐标及其对应的属性(如半径、长、高、宽、面积、周长、体积等)。当然,视情况去定义。 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4725355.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

Objective-C:异常的处理

1 // 2 // main.m 3 // 03-动态绑定 4 // 5 // Created by mac on 15/8/11. 6 // Copyright (c) 2015年. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import "Fraction.h" 11 #import "Complex.h" 12 #import "MyException.h" 13 int main(int argc, const char * argv[]) 14 { 15 @autoreleasepool 16 { 17 //测试分数类 18 Fraction *f1 = [[Fraction alloc]initWithNumerator:1 19 addDenominator:2]; 20 [f1 print]; 21 22 Fraction *f2 = [[Fraction alloc]initWithNumerator:2 23 addDenominator:3]; 24 [f2 print]; 25 26 Fraction *f3 = [f1 add:f2]; 27 [f3 print]; 28 29 //测试复数类 30 Complex *c1 = [[Complex alloc]initWithReal:5.0 andImag:3.0]; 31 [c1 print]; 32 33 Complex *c2 = [[Complex alloc]initWithReal:4.3 andImag:2.5]; 34 [c2 print]; 35 36 Complex *c3 = [c1 add: c2]; 37 [c3 print]; 38 39 40 //测试动态绑定 41 id pObj = nil; 42 pObj = f3; 43 [f3 print]; 44 45 pObj = c3; 46 [c3 print]; 47 48 MyException *pe; 49 @try//将容易出现异常的代码放在try代码块里面 50 { 51 id arr[3] = {c1,f1,@""}; 52 for(int i=0;i<3;i++) 53 { 54 [arr[i] print]; 55 pe = [[MyException alloc] initWithName:@"MyException" reason:@"test" userInfo:nil]; 56 @throw pe;//主动抛出自定义的异常 57 } 58 NSLog(@"try last statement");//在出现异常后会跳过@try中后续的代码,不会执行。 59 } 60 @catch(MyException *e)//捕捉自定义的异常 61 { 62 NSLog(@"MyException:%@",[e reason]); 63 } 64 @catch(NSException *e)//系统捕捉异常 65 { 66 NSLog(@"%@",[e reason]); 67 } 68 @finally//最终处理 69 { 70 NSLog(@"finally"); 71 } 72 //异常处理完,后续代码会继续执行 73 NSLog(@"last statement!"); 74 } 75 return 0; 76 } 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4721504.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

Objective-C:Objective-C:文件中一些对目录进行操作的函数

IO文件中,一些对目录进行操作的函数:获取、切分、组合 一些对目录进行操作的函数: 获取用户的姓名:(NSString*)NSUserName() ; ———>NSString *Str = NSUserName() 获取home目录路径:(NSString*)NSHomeDirectory() 获取临时文件夹路径:(NSString*)NSTemporaryDirectory() 一些对目录进行操作的方法: 将路径切成部分:- (NSArray *)pathComponents ———>NSArray *paths = [path pathComponents] 将部分组合成路径:+ (NSString*)pathWithComponents:(NSArray*)components 判断是不是绝对路径:- (BOOL)isAbsolutePath 获取路径最后一部分:- (NSString*)lastPathComponent 删除最后一部分:- (NSString*)stringByDeletingLastPathComponent 获取扩展名:- (NSString*)pathExtension 删除扩展名:- (NSString*)stringByDeletingPathExtension 添加路径:- (NSString*)stringByAppendingPathComponent:(NSString*)str 添加扩展名:- (NSString*)stringByAppendingPathExtension:(NSString*)str //1、获取home目录的路径 1 NSString *homeDir = NSHomeDirectory(); 2 NSLog(@"home:%@",homeDir); 2015-08-24 21:17:28.727 05-path[992:45425] home:/Users/mac //2、获取临时文件夹的路径 1 NSString *tempDir = NSTemporaryDirectory(); 2 NSLog(@"home:%@",tempDir); 2015-08-24 21:17:28.728 05-path[992:45425] home:/var/folders/dx/481d4y4j4430hfty5zr_1gfh0000gn/T/ //3、将路径分成部分 1 NSArray *paths = [tempDir pathComponents]; 2 NSLog(@"%@",paths); 2015-08-24 21:17:28.728 05-path[992:45425] ( "/", var, folders, dx, "481d4y4j4430hfty5zr_1gfh0000gn", T, "/" ) //4、将部分再拼成路径 1 NSString *path = [NSString pathWithComponents:paths]; 2 NSLog(@"%@",path); 2015-08-24 21:17:28.729 05-path[992:45425] /var/folders/dx/481d4y4j4430hfty5zr_1gfh0000gn/T //5、在路径上添加文件 1 NSString *fileNamePath = [path stringByAppendingPathComponent:@"1.txt"]; 2 NSLog(@"fileName:%@",fileNamePath); 2015-08-24 21:17:28.729 05-path[992:45425] fileName:/var/folders/dx/481d4y4j4430hfty5zr_1gfh0000gn/T/1.txt //6、获取路径的最后一部分 1 NSString *fileName = [fileNamePath lastPathComponent]; 2 NSLog(@"fileName:%@",fileName); 2015-08-24 21:17:28.729 05-path[992:45425] fileName:1.txt //7、获取文件的扩展名 1 NSString *extension = [fileNamePath pathExtension]; 2 NSLog(@"extension:%@",extension); 2015-08-24 21:17:28.729 05-path[992:45425] extension:txt Program ended with exit code: 0 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4755962.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

UIWebView without a .xib Objective-C[转]

Add a UIWebView programmatically without using a .xib file. This goes in the views controller. Original fromhttp://www.gibsontang.com/?p=176(Big thanks man!) - (void) initUIWebView { NSLog(@”DetailViewController->initUIWebView {“); UIWebView *aWebView; // init and create the UIWebView aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)]; aWebView.autoresizesSubviews = YES; aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth); //set the web view delegates for the web view to be itself [aWebView setDelegate:self]; //Set the URL to go to for your UIWebView NSString *urlAddress = @”http://www.google.com”; //Create a URL object. NSURL *url = [NSURL URLWithString:urlAddress]; //URL Requst Object NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; //load the URL into the web view. [aWebView loadRequest:requestObj]; //add the web view to the content view [self.view addSubview:aWebView]; [aWebView release], aWebView = nil; } - (void)viewDidLoad { [self initUIWebView]; // <<<<<<<<<<<< this calls the UIWebView Function [super viewDidLoad]; } 欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!' 本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/archive/2011/11/22/2258908.html ,如需转载请自行联系原作者

优秀的个人博客,低调大师

Objective-C之预处理程序

预处理指令使用#内标记。 一 : #define语句 用来定义宏。例如定义: #define TRUE 1 解释 : 定义了一个宏名称为TRUE,并使它的值为1.那么之后TRUE就可以用于程序中所有需要值为1的常量的地方。 测试实例: 在DefineDemo.h中定义宏 : #define TRUE 1 。注意一般将其定义在最上面,如下图(定义在了#import的上面) 调用的地方:可以在任何地方调用。 ①:在DefineDemo.m中调用: ②:在主程序main.m中调用: 结果: 注意 : 宏是只读的 二 : 条件编译之 #ifdef , #endif , #else , #ifndef , #elif #ifdef : 如果存在编译参数 #ifndef : 如果不存在编译参数 例如:修改了下DefineDeno.m中Print方法的实现,如下: 我没有定义编译参数AINY , 则会打印Ainy not。结果如下,确实如此: 那么问题来了,如何定义一个编译参数AINY呢 ?????? 在Build Settings中: 再次运行结果,则是打印TRUE值1 显然 : #ifdef , #ifndef 和Build Settings中的如上图所示的配置是相关的 三 : #if 是判断常量的 如: 四 : 编译参数之 #undef : 消除某个编译参数 如 : #undef AINY ,在这句执行后, 所有#if AINY 及 #if defined(AINY) 都为FALSE 本文转自Aonaufly51CTO博客,原文链接:http://blog.51cto.com/aonaufly/2049027,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

用户登录
用户注册
修改密码
忘记密码