一、入門教程
二、類基本用法
@interface MyObject : NSObject { //聲明interface
int memberVar1; // 實體變量 默認protected屬性
id memberVar2;
@public //自定義public屬性
NSString *name;
}
+(return_type) class_method; // 類方法,類似static
-(return_type) instance_method1; // 實例方法
-(return_type) instance_method2: (int) p1;
-(return_type) instance_method3: (int) p1 andPar: (int) p2;
@end
@implementation MyObject { //定義implementation
int memberVar3; //默認private屬性
}
+(return_type) class_method {
.... //method implementation
}
-(return_type) instance_method1 {
....
}
-(return_type) instance_method2: (int) p1 {
....
}
-(return_type) instance_method3: (int) p1 andPar: (int) p2 {
....
}
@end
調用方法示例:
MyObject *justTest =[MyObject alloc];
//實例方法
[justTest instance_method1 ];
...
//類方法可直接使用類名調用//
[MyObject class_method];
協議(純虛繼承,需要實現接口的方法)
@protocol Locking //協議
- (void)lock;
- (void)unlock;
@end
@interface SomeClass : SomeSuperClass <Locking>
@end
@implementation SomeClass
- (void)lock {
// 實現lock方法...
}
- (void)unlock {
// 實現unlock方法...
}
@end
三、協議與委託
#import <Foundation/Foundation.h>
@protocol PrintProtocolDelegate
- (void)processCompleted;
@end
//委託的作用:一個類如PrintClass類調用結束後,通知使用它的另外一個類SampleClass
@interface PrintClass :NSObject {
id delegate;
}
- (void) printDetails;
- (void) setDelegate:(id)newDelegate;
@end
@implementation PrintClass
- (void)printDetails {
NSLog(@"Printing Details");
[delegate processCompleted];
}
- (void) setDelegate:(id)newDelegate {
delegate = newDelegate;
}
@end
@interface SampleClass:NSObject<PrintProtocolDelegate>
- (void)startAction;
@end
@implementation SampleClass
- (void)startAction {
PrintClass *printClass = [[PrintClass alloc]init];
[printClass setDelegate:self];
[printClass printDetails]; }
-(void)processCompleted {
NSLog(@"Printing Process Completed");
}
@end
int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
SampleClass *sampleClass = [[SampleClass alloc]init];
[sampleClass startAction];
[pool drain];
return 0;
}
四、動態調用
- setMyValue:(id) foo; //id形態表示參數"foo"可以是任何類的實例
- setMyValue:(id <aProtocol>) foo; //id<aProtocol>表示"foo"可以是任何類的實例,但必須採納"aProtocol"協議
- setMyValue:(NSNumber*) foo; //該聲明表示"foo"必須是"NSNumber"的實例
六、iOS - NSLog打印(精準打印)
https://www.jianshu.com/p/5b5798976feb
NSLog(@“打印字符串:%@”,name);
NSLog(@“打印整形:%i”,number);//或者 %li ; %ld ; %d
NSLog(@“打印字符:%c”,c);
NSLog(@“打印單浮點數:%f”,f);
NSLog(@“打印精度浮點數:%.2f”,f);
NSLog(@“BOOL–b-->%@”,isYES?@“YES”😡“NO”);//打印布爾類型
七、字符串基本操作
https://blog.csdn.net/wangyurui_wyr/article/details/52457534
NSString *str1 = @"mingtian*";
NSString *str2 = @"nihao*";
NSString *str3 = [NSString stringWithFormat:@"%@%@",str1,str2];
八、數據轉換
Object - C 入門 之 數據類型詳解https://blog.csdn.net/shulianghan/article/details/38544659
NSDictionary、NSMutableDictionary的各種實用的用法以及枚舉
https://blog.csdn.net/ylwdi/article/details/46814523
https://blog.csdn.net/u014220518/article/details/50945177
1、 Int 轉NSNumber
int iValue;
NSNumber *number = [NSNumber numberWithInt:iValue];
2、 NSNumber 轉Int
int myInt = [number intValue];;
3、NSString轉int
NSString *stringInt = @“120”;
int ivalue = [stringInt intValue];
4、int轉NSString
NSString *string = [NSString stringWithFormat:@"%d",ivalue];
NSString用法
//1.基本用法
NSString *greeting = @"Hello";
//2.nstring添加字符串
NSString* string; // 結果字符串
NSString* string1, string2; //已存在的字符串,需要將string1和string2連接起來
//方法1.
string = [NSString initWithFormat:@"%@,%@", string1, string2 ];
//方法2.
string = [string1 stringByAppendingString:string2];
//方法3 .
string = [string stringByAppendingFormat:@"%@,%@",string1, string2];
九、# \Log
#import <Foundation/Foundation.h>
int main()
{
NSLog(@“File :%s\n”, FILE );
NSLog(@“Date :%s\n”, DATE );
NSLog(@“Time :%s\n”, TIME );
NSLog(@“Line :%d\n”, LINE );
NSLog(@“ANSI :%d\n”, STDC );
return 0;
}
十、類別(使用已有的類,爲他增加新的方法屬性)
#import <Foundation/Foundation.h>
@interface NSString(MyAdditions)
+(NSString *)getCopyRightString; //類似NNString,爲該類增加方法
@end
@implementation NSString(MyAdditions) +(NSString *)getCopyRightString {
return @“Copyright y ii bai.com 2019”;
}
@end
int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *copyrightString = [NSString getCopyRightString];
NSLog(@“Accessing Category: %@”,copyrightString); [pool drain];
return 0;
}
十一、擴展(類似類別,不過只能使用正在寫的類)
#import <Foundation/Foundation.h>
@interface SampleClass : NSObject {
NSString *name;
}
- (void)setInternalID;
- (NSString *)getExternalID;
@end
@interface SampleClass() { //擴展上面的了類,和類別作用一樣,區別就是擴展需要在同一文檔中。
NSString *internalID;
}
@end
@implementation SampleClass
- (void)setInternalID {
internalID = [NSString stringWithFormat: @“UNIQUEINTERNALKEY%dUNIQUEINTERNALKEY”,arc4random()%100];
} - (NSString *)getExternalID {
return [internalID stringByReplacingOccurrencesOfString: @“UNIQUEINTERNALKEY” withString:@""];
}
十二、複合對象(把已有的對象類拿出來,封裝爲另一個類,進行操作)
oc調用c++
https://blog.csdn.net/u010008647/article/details/78318628
警告:
used as the name of the previous parameter rather than as part of the selector 多爲傳入多個變量沒有空格引起,在第二變量的冒號前面加個空格既可以解決。
ios調用c++出現iostream file not found錯誤
解決方法:將調用的m文件改爲mm文件,將ViewController.m改爲ViewController.mm
https://blog.csdn.net/jasonblog/article/details/7880841