IOS入门-基础语法

常量与变量

#import <Foundation/Foundation.h>
#define count1 124
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //常量即为恒定不变的量 是不可以修改的,是只读的 比如圆周率 一年12个月
        const int num=4;
        
        //变量是可以修改的,可以读取的 
        int score=80;
        score=90NSLog(@"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);
//        NSLog(@"除的结果是 = %d",a/b);
//        NSLog(@"取余的结果是 = %d",a%b);
        int c = 8;
        int d = c++; //先c自增1,在将c赋值给d
        ind e = ++c;//c先赋值给e,再自增1
        NSLog(@"d的值 = %d",d);
   
        
        //关系运算符 与(&)(||)非(!) 大于(>)小于(<)等于(==)不等于(!=)
//        NSLog(@"结果是%d",!(a||b));
        int c = 0;
//        c-=a;//c=c-a
//        NSLog(@"c = %d",c);
        //三元运算
        int d = c>0?10:20;
        NSLog(@"d = %d",d);
    }
    return 0;
    
}

分支语句

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //分支语句
        int score=70;
        if(score<60){
        NSLog(@"不及格的分数 = %d",score);
        }else{
        NSLog(@"及格的分数 = %d",score);
        }
        //switch 后面必须是常量    ,case后面必须是常量
        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 <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
        //循环
        //While循环
//        int a = 10;
//        while (a<20) {
//            NSLog(@"a = %d",a);
//            a++;
//        }
        
        //Do…while 循环
//        do{
//            a++;
//            NSLog(@"a = %d",a);
//
//        }while (a<20);
        
        //For循环
//        for (int a = 10; a<20; a++) {
//            NSLog(@"a = %d",a);
//        }
        
        //break
//        int a = 10;
//        while (a<20) {
//            NSLog(@"a = %d",a);
//            a++;
//            if (a>15) {
//                break;//这里我们就跳出了循环
//            }
//        }
//
//        NSLog(@"我出来了,不在循环里面了");
        
        //Continue
//        int a = 10;
//        do{
//           if (a == 15) {
//                a = a + 1;
//                continue;
//            }
//            NSLog(@"a = %d",a);
//            a++;
//        }while(a<20);
        
        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];
//    NSLog(@"tempStr1 = %@",tempStr1);
    
    //截取中间的一个区域的字符
    NSRange range;
    range.location = 4;
    range.length = 6;
    NSString *temStr2 = [str substringWithRange:range];
//    NSLog(@"temStr2 = %@",temStr2);
    
    //替换字符串中的字符
    NSString *tempStr3 = [str stringByReplacingOccurrencesOfString:@"C" withString:@"A"];
//    NSLog(@"tempStr3 = %@",tempStr3);
    
    //替换某一段字符
    NSRange range1 = NSMakeRange(4, 6);
    NSString *tempStr4 = [str stringByReplacingCharactersInRange:range1 withString:@"Middle"];
//    NSLog(@"111");
    
    //分割字符串
    NSString *str1 = @"111,222,333,444";
    NSArray *tempArray = [str1 componentsSeparatedByString:@","];
//    NSLog(@"tempArray = %@",tempArray);
    
    NSString *str2 = @"name:xiaoming;age:11;height:170";
    NSArray *tempArray2 = [str2 componentsSeparatedByString:@";"];
//    NSLog(@"tempArray2 = %@",tempArray2);
    
    //字符串的拼接
    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];
//    NSLog(@"tempStr6 = %@",tempStr6);
    
    
}


@end

数据类型

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //整数 int NSInteger  1,2,3
    //小数 float CGFloat  1.2,1.3
    //字符 char 'a','b','c'
    //数字 NSNumber 1,2,3
    //布尔 BOOL 0,1
    
    
    NSInteger a;
    a = 1;
    NSInteger b = 2;
    
    CGFloat c = 1.3;
    CGFloat d;
    d = 1.4;
    
//    NSNumber *number = [[NSNumber alloc]init];
    NSNumber *number = [NSNumber numberWithInteger:10];
//    NSLog(@"number = %@",number);
    
    b = a + number.integerValue;
//    NSLog(@"b = %d",b);
    
    NSNumber *number1 = [NSNumber numberWithFloat:1.6];
    c = c + number1.floatValue;
//    NSLog(@"c = %f",c);
    
    NSNumber *number2 = [NSNumber numberWithBool:YES];
//    NSLog(@"number2 = %@",number2);
    BOOL m = number2.boolValue;
//    NSLog(@"m = %d",m);
    
    NSString *str = @"100";
    NSInteger n = str.integerValue;
    NSLog(@"n = %d",n);
}


数组

//NSArray *array=[[NSArray alloc]init];
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]);//通过key 来获取value
    }
}


@end

创建类

创建.h头文件

#import <Foundation/Foundation.h>

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


类的继承

父类

//父类
//  Animal.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Animal: NSObject

@property(nonatomic,assign)NSInteger age;

@end

NS_ASSUME_NONNULL_END

//父类
//  Animal.m
//  类的继承

#import "Persion.h"

@implementation Persion

@end

子类

//子类
//  Dog.h

#import <Foundation/Foundation.h>
#import "Animal.h"
NS_ASSUME_NONNULL_BEGIN

@interface Dog: Animal

@property(nonatomic,assign)NSInteger age;

@end

NS_ASSUME_NONNULL_END

//子类
//  Dog.m
//  类的继承

#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];
    // Do any additional setup after loading the view, typically from a nib.
    //内存
    //手动管理内存
    //自己生产的对象,自己持有。
    //核心是谁创建谁释放,计数器
    //-1  release
    Object *object = [[Object alloc]init]; +1
    [object release]; -1
}


@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值