安裝Homebrew
試了很多種方法啊,最後手動安裝,真心好文推薦
https://zhuanlan.zhihu.com/p/111014448
接口 MyClass.h
@interface MyClass : NSObject
{
//成員變量字段
@public //自己和外部都可以訪問
int age;
@private //自己內部訪問
float height;
@protected //子類和自己可以訪問
NSString *name;
}
//對象方法和類的方法
//方法名以init開頭的方法 一般說構造方法
-(id) init;
-(id) initWithAge:(int)newAge;
-(id) initWithAge:(int)newAge andHight:(float)newHight;
//set
-(void) setAge:(int)newAge;
-(void) setHight:(float)newHight;
-(void) setName:(NSString *)newName;
//get
-(int) getAge;
-(float) getHight;
-(NSString *) getName;
@end
執行MyClass.m
#import "MyClass.h"
@implementation MyClass
{
//寫成員變量
}
-(id) init
{
if (self == [super init]) {
age = 23;
height = 168.5;
name = @"jiangnan";
//super 表示父類 init方法 第一步分配內存空間 第二步內存空間指向self
//self 表示對象本身
}
return self;
}
- (id)initWithAge:(int)newAge
{
if (self = [super init]) {
age = newAge;
height = 168.5;
name = @"jiangnan";
}
return self;
}
- (id)initWithAge:(int)newAge andHight:(float)newHight
{
if (self = [super init]) {
age = newAge;
height = newHight;
name = @"jiangnan";
}
return self;
}
- (void)setAge:(int)newAge
{
age = newAge;
}
- (void)setHight:(float)newHight
{
height = newHight;
}
- (void)setName:(NSString *)newName
{
name = newName;
}
- (int)getAge
{
return age;
}
- (float)getHight
{
return height;
}
- (NSString *)getName
{
return name;
}
@end
但是 實際中get方法直接 變量名 去掉get
self:當前方法誰在調用, self就是誰。
創建對象main.m
MyClass *class = [[MyClass alloc] init];
MyClass *class1 = [[MyClass alloc] initWithAge:(age) andHight:(hight)];
打印日誌
NSLog(@"age is %d, hight is %0.1f, name is %@",class->age,[class getHight],[class getName]);
輸出結果:
age is 23, hight is 168.5, name is jiangnan
class->age 指針 public 可以直接調用
同樣面向對象思維
set get 數據封裝 通過定義方法或函數去操作成員變量或屬性
封裝的目的:提高代碼安全性。提高代碼可行性和效率。
[class setName:@"Eileen"];
[class setHight:167.5];
繼承:可以實現屬性的擴展
Student.h 繼承 MyClass
#import "MyClass.h"
@interface Student : MyClass
{
@public
NSString *sex;
}
-(id)init;
-(void) setSex:(NSString *)newSex;
-(NSString *) getSex;
@end
Student.m
#import "Student.h"
@implementation Student
{
}
- (id)init
{
self = [super init];
if (self) {
age = 22;
sex = @"女";
}
return self;
}
- (void)setSex:(NSString*)newSex
{
sex = newSex;
}
- (NSString *)getSex
{
return sex;
}
@end
main.m
Student *student = [[Student alloc]init];
[student setName:@"xiaofei"];
[student setHight:167.4];
NSLog(@"age is %d, hight is %0.1f, name is %@ , sex is %@",[student getAge],[student getHight],[student getName],[student getSex]);
輸出結果:
age is 22, hight is 167.4, name is xiaofei , sex is 女
也可以進行重寫
上述是Objective-C的面向對象思想的複習,set、get 這種思維方式 以及被 @property 這個方法直接替代,可以節省編寫的代碼量。自動生成set、get 方法。
@interface Person : NSObject
@property (nonatomic , strong) NSString *name;
@property (nonatomic , assign) int age;
@property (nonatomic , assign) float hight;
@end
strong object-C 中的數據類型
assign 其他數據類型
nonatomic 非原子性
賦值獲取:
- (void) getPropert
{
_name = @"jiangnan";
_hight = 172.2;
self.age = 22; //相當於set
}
self.name 相當於get
類別
類別和繼承的區別
相同目的:都是無法滿足需求,需要更新屬性或方法而擴展。
不同點:繼承即可擴展成員屬性、變量,也可以添加方法。類別只能添加方法。繼承添加的新屬性和新方法,都可以在子類中調用。而類添加的新方法可以在原對象 直接調用。
Person+extention.h
@interface Person (extention)
-(void) studying;
@end
Person+extention.m
main.m
Person *p = [[Person alloc] init];
[p studying];
輸出結果:
正在學習!
數組
數組可以裝任意對象;數組中的每一個對象,其實是指向該對象的地址
Preson *p = [[Preson alloc]init];
NSArray *arr = [[NSArray alloc] initWithObjects:@"one", @"two", @"three",p,nil];
NSLog(@"%@", arr);
輸出結果:
(
one,
two,
three,
"<Preson: 0x1048a93d0>"
)
遍歷(3種)
//數組的遍歷
//枚舉器遍歷法
NSEnumerator *enumerator = [arr objectEnumerator];
id obj;
while (obj = [enumerator nextObject]) {
NSLog(@"%@", obj);
}
//快速遍歷法
for(obj in arr){
NSLog(@"%@", obj);
}
//i遍歷法
id teObj = [arr objectAtIndex:0];
NSInteger i = 0;
for (i; i< [arr count]; i++) {
NSLog(@"%@", [arr objectAtIndex:i]);
}
集合類(如:NSArray、NSSet、NSDictionary等)均可獲取到NSEnumerator, 該類是一個抽象類,沒有用來創建實例的公有接口。NSEnumerator的nextObject方法可以遍歷每個集合元素,結束返回nil,通過與while結合使用可遍歷集合中所有項。
可變數組
//可變數組初始化
NSMutableArray *arr1 = [[NSMutableArray alloc] init];
//增
[arr1 addObject:@"One"];
[arr1 addObject:@"Two"];
[arr1 addObject:@"Three"];
NSLog(@"%@", arr1);
//刪
[arr1 removeObject:@"Two"];
NSLog(@"%@", arr1);
//交換
[arr1 exchangeObjectAtIndex:0 withObjectAtIndex:1];
NSLog(@"%@", arr1);
字典
字典當中的所有的key-value 都是無序的
//字典
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"one", @"1",@"two", @"2", nil];
NSString *key = @"1";
NSString *value = [dic objectForKey:key];
NSLog(@"%@", value);
可變字典
//可變字典
NSDictionary *dic1 = @{
@"1":@"one", @"2":@"two", @"3":@"three"
};
NSLog(@"%@", dic1);
輸出結果:
{
1 = one;
2 = two;
3 = three;
}
遍歷
NSEnumerator *enm = [dic1 objectEnumerator];
while (obj = [enm nextObject]) {
NSLog(@"%@", obj);
}
for(obj in dic1){
NSLog(@"%@", obj);
NSLog(@"%@", [dic1 objectForKey:obj]);
}