一、入门教程
入门教程1
入门教程2
二、类基本用法
@interface MyObject : NSObject { //声明interfaceint 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 { //定义implementationint 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打印(精准打印)
/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”);//打印布尔类型
七、字符串基本操作
/wangyurui_wyr/article/details/52457534NSString *str1 = @"mingtian*";NSString *str2 = @"nihao*";NSString *str3 = [NSString stringWithFormat:@"%@%@",str1,str2];
八、数据转换
Object - C 入门 之 数据类型详解/shulianghan/article/details/38544659
NSDictionary、NSMutableDictionary的各种实用的用法以及枚举
/ylwdi/article/details/46814523
/u014220518/article/details/50945177
1、 Int 转NSNumberint iValue;NSNumber *number = [NSNumber numberWithInt:iValue];2、 NSNumber 转Intint myInt = [number intValue];;3、NSString转intNSString *stringInt = @“120”;int ivalue = [stringInt intValue];4、int转NSStringNSString *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 ”;
}
@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++
/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
/jasonblog/article/details/7880841