基础语法
- 常量与变量
- 运算符
- 分支语句
- 循环语句
- 字符串
- 数据类型
- 数组
- 字典
- 创建类
- 类的继承
- 内存管理
常量与变量
#import
#define count1 124
int main(int argc, const char * argv[]) {
@autoreleasepool {
const int num=4;
int score=80;
score=90;
NSLog(@"score= %d",score);
}
return 0;
}
运算符
int main(int argc, const char * argv[]) {
@autoreleasepool {
int a = 5;
int b = 3;
int sum=a+b;
NSLog(@"求和结果 = %d",sum);
int c = 8;
int d = c++;
ind e = ++c;
NSLog(@"d的值 = %d",d);
int c = 0;
int d = c>0?10:20;
NSLog(@"d = %d",d);
}
return 0;
}
分支语句
#import
int main(int argc, const char * argv[]) {
@autoreleasepool {
int score=70;
if(score<60){
NSLog(@"不及格的分数 = %d",score);
}else{
NSLog(@"及格的分数 = %d",score);
}
int a = 10;
switch (a) {
case 1:
{
NSLog(@"a = 1");
}
break;
case 2:
{
NSLog(@"a = 2");
}
break;
case 5:
{
NSLog(@"a = 5");
}
break;
default:
NSLog(@"默认");
break;
}
}
return 0;
}
循环语句
#import
int main(int argc, const char * argv[]) {
@autoreleasepool {
for(int i = 0;i<10;i++){
NSLog(@"遍历%d",i);
}
}
return 0;
}
字符串
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)NSString *name;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *name=@"张三";
NSString *str = @"HeadCenterEndCCC";
NSString *tempStr = [str substringToIndex:4];
NSLog(@"tempStr = %@",tempStr);
NSString *tempStr1 = [str substringFromIndex:str.length - 3];
NSRange range;
range.location = 4;
range.length = 6;
NSString *temStr2 = [str substringWithRange:range];
NSString *tempStr3 = [str stringByReplacingOccurrencesOfString:@"C" withString:@"A"];
NSRange range1 = NSMakeRange(4, 6);
NSString *tempStr4 = [str stringByReplacingCharactersInRange:range1 withString:@"Middle"];
NSString *str1 = @"111,222,333,444";
NSArray *tempArray = [str1 componentsSeparatedByString:@","];
NSString *str2 = @"name:xiaoming;age:11;height:170";
NSArray *tempArray2 = [str2 componentsSeparatedByString:@";"];
NSString *str3 = @"1111";
NSString *str4 = @"2222";
NSString *tempStr5 = [NSString stringWithFormat:@"%@-%@",str3,str4];
NSLog(@"tempStr5 = %@",tempStr5);
NSString *phoneNUmber = @"123456";
NSString *resultStr = [NSString stringWithFormat:@"手机号码:%@",phoneNUmber];
NSLog(@"resultStr = %@",resultStr);
int a = 5;
NSString *tempStr6 = [NSString stringWithFormat:@"%@%d",str3,a];
}
@end
数据类型
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSInteger a;
a = 1;
NSInteger b = 2;
CGFloat c = 1.3;
CGFloat d;
d = 1.4;
NSNumber *number = [NSNumber numberWithInteger:10];
b = a + number.integerValue;
NSNumber *number1 = [NSNumber numberWithFloat:1.6];
c = c + number1.floatValue;
NSNumber *number2 = [NSNumber numberWithBool:YES];
BOOL m = number2.boolValue;
NSString *str = @"100";
NSInteger n = str.integerValue;
NSLog(@"n = %d",n);
}
数组
NSArray *array=[[NSArray alloc] initWithObjects:@"a",@"b",@"c"];
NSLog(@"array = %@",array);
NSMutableArray *mArray=[NSMutableArray alloc]init];
NSString *a=@"123";
NSString *b=@"456";
[mArray addObject:a];
[mArray addObject:b];
NSMutableArray *mArrayWith=[[NSMutableArray arrayWithOBjects:@"2",@"3",@"4"]];
字典
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *dic=[[NSDictionary alloc] init];
NSDictionary *dicWith = [NSDictionary dictionaryWithObject:@"12" forKey:@"age"];
NSDictionary *dic1 = @{
@"name":@"张三",
@"age":@"16"
};
NSDictionary *dic2 = [NSDictionary dictionaryWithDictionary:dic1];
NSDictionary *dic3 = [NSDictionary
NSMutableDictionary *dic4=[[]NSMutableDictionary alloc]init];
[dic4 setObject:@"张三" forKey:@"name"];
[dic4 removeObject:@"张三" forKey:@"name"];
for (id key in dic4) {
NSLog(@"key:%@ value:%@",key,[dic4 objectForKey:key]);
}
}
@end
创建类
创建.h头文件
#import
NS_ASSUME_NONNULL_BEGIN
@interface User: NSObject
@property(nonatomic,strong) NSString *name;
@property(nonatomic,assing) int*age;
-(void) functionName;
-(void) functionWithAge:(NSInteger)age;
-(NSInteger) geAge;
@end
NS_ASSUME_NONNULL_END
.m类方法实现的文件
@interface User()
{
NSString *phoneNum;
}
#import "User.h"
@implementation User
-(instancetype)init{
self=[super init];
if(self){
self.name=@"张三";
self.age=11;
self->phoneNum=@"12345678912":
}
return self;
}
-(void) functionName{
NSLog(@"我的名字为:%@",self.name);
}
-(void) functionNameWithAge:(NSInteger)age{
self.age=age;
NSLog(@"我的年龄是:%d",self.age);
}
-(NSInteger) getAge(){
return self.age;
}
-(void)dealloc{
NSLog(@"释放资源")
}
@end
类的继承
父类
#import
NS_ASSUME_NONNULL_BEGIN
@interface Animal: NSObject
@property(nonatomic,assign)NSInteger age;
@end
NS_ASSUME_NONNULL_END
#import "Persion.h"
@implementation Persion
@end
子类
#import
#import "Animal.h"
NS_ASSUME_NONNULL_BEGIN
@interface Dog: Animal
@property(nonatomic,assign)NSInteger age;
@end
NS_ASSUME_NONNULL_END
#import "Dog.h"
@implementation Persion01
- (instancetype)init
{
self = [super init];
if (self) {
self.age= 12;
}
return self;
}
@end
内存管理
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Object *object = [[Object alloc]init]; +1
[object release]; -1
}
@end