对于最近才加入博客大家族的我,趁现在时间富裕,决定带着大家一起来撸一遍Objective-C。
言归正传,直接上代码:
上代码之前,先做点简单的铺垫
//在 iOS工程中:
// .m是oc 的后缀
// .c是c 语言的后缀。
// .cpp是c++的后缀
// .mm兼容oc,c和c++的后缀
// import和include 具有相同的功能,优点是可以避免同一个文件被重复包含/引入(查重)
//Foundation是OC的基本库,里面封装了很多的OC的数据类型和一些常用的方法(函数),OC程序离开了Foundation这个库不能工作
//使用<>包含头文件或者库,说明这个头文件或者库是系统的头文件和库
//使用""包含头文件或者库,说明这个头文件或者库是自定义或者第三方的
//类:类的声明和类的实现
//类的声明:@interface时一个类的关键字
//NSObject是oc中所有类直接或者间接的父类,也是根类
//类的声明
//@interface + 类名 : 父类名{
//成员变量 (属性,字段)
//}@end
//@interface Person : NSObject{
// char name[20];
// int arge;
// int face;
//}
//
//@end
//-(void)func; //这个是oc最简单的方法(没有参数和返回值)
//- 代表这个方法是对象方法
//+ 代表这个方法是类方法
//oc中返回值类型用()
//在没有参数时函数名后面括号必须省略
//类的实现 @implementation +类名 +成员方法的实现 +@end
//oc类创建的对象中在外部函数(main)中不能直接使用成员变量
//内部函数 ->
下面介绍几个Objective-C (OC)中的数据类型:
1、NSNumber
首先开一下官方文档中的介绍
@interface NSNumber : NSValue
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithChar:(char)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedChar:(unsigned char)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithShort:(short)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedShort:(unsigned short)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithInt:(int)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedInt:(unsigned int)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithLong:(long)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedLong:(unsigned long)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithLongLong:(long long)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedLongLong:(unsigned long long)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithFloat:(float)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithDouble:(double)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithBool:(BOOL)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0) NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0) NS_DESIGNATED_INITIALIZER;
@property (readonly) char charValue;
@property (readonly) unsigned char unsignedCharValue;
@property (readonly) short shortValue;
@property (readonly) unsigned short unsignedShortValue;
@property (readonly) int intValue;
@property (readonly) unsigned int unsignedIntValue;
@property (readonly) long longValue;
@property (readonly) unsigned long unsignedLongValue;
@property (readonly) long long longLongValue;
@property (readonly) unsigned long long unsignedLongLongValue;
@property (readonly) float floatValue;
@property (readonly) double doubleValue;
@property (readonly) BOOL boolValue;
@property (readonly) NSInteger integerValue NS_AVAILABLE(10_5, 2_0);
@property (readonly) NSUInteger unsignedIntegerValue NS_AVAILABLE(10_5, 2_0);
@property (readonly, copy) NSString *stringValue;
- (NSComparisonResult)compare:(NSNumber *)otherNumber;
- (BOOL)isEqualToNumber:(NSNumber *)number;
- (NSString *)descriptionWithLocale:(nullable id)locale;
@end
@interface NSNumber (NSNumberCreation)
+ (NSNumber *)numberWithChar:(char)value;
+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;
+ (NSNumber *)numberWithShort:(short)value;
+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
+ (NSNumber *)numberWithInt:(int)value;
+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
+ (NSNumber *)numberWithLong:(long)value;
+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
+ (NSNumber *)numberWithLongLong:(long long)value;
+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
+ (NSNumber *)numberWithFloat:(float)value;
+ (NSNumber *)numberWithDouble:(double)value;
+ (NSNumber *)numberWithBool:(BOOL)value;
+ (NSNumber *)numberWithInteger:(NSInteger)value;
+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value;
@end
大家看到官方文档可能会感到头疼,天了撸,怎么这么多啊,这要怎么记啊,其实大家不用紧张,小编第一次看到时也是很捉急,不过大家可以先记下小编给你们标记为红色的部分,因为他们是真爱,真爱,真爱(重要的话说三遍)。下面为大家带来几个小例子大家就不会在迷糊了