ios开发-OC核心语法的学习-1

来源:互联网 发布:新东方网络课程百度云 编辑:程序博客网 时间:2024/05/24 02:58


今天我学习了OC核心语法的第一部分,以下是学习内容:

 1.NSString的学习:

    1)NSString是定义OC中字符串的关键字,格式如下:

        NSString *_name = @"jack";

       或者:

        [NSString stringWithFormat:@"jack"]

    2)创建OC对象的另一种方法:

        NSString *str = [NSString stringWithformat:@"my name is %@, age is %d", _name, _age];

    3)length的用法:

       是用来计算字符串的字数,注意是字数.

       格式如下:

        [str length];//用对象去调用length的方法

  2.点语法的学习:

       点语法实际上还是setget方法的调用,格式如下:

        p.age = 10;

       等价于 [p setAge:10];// set方法  或者 p->age = 10;

        int a = p.age;

       等价于  [p age];// get方法

  3.propertysynthesize的学习:

       在函数的声明和实现中,我们之前用的是setget方法,现在有了新的代替方法.

        1)property:在开发中可以代替setget方法来声明方法,具体如下:

            @interface Person : NSObject

            {

                int _age;

     

                }

            @end

           使用setget方法如下:

            - (void)setAge:(int)age;

            - (int)age;

     

           而使用property则可以写成:

            @property int age;

        

        2)synthesize:在开发中可以代替setget方法来实现方法:

            @implementation Person

            - (void)setAge:(int)age

            {

                _age = age;

            }

            - (int)age 

            {

                return _age;

     

            }

            @end

           而使用synthesize可以写成:

            @synthesize age = _age;

        3)property的应用:

           在开发中,property可以代替方法的声明和实现:

           例如: @property int age;

           表示: 1)在成员变量中创建了一个_age的实例变量;

                  2)声明和setget方法;

                  3)实现了setget方法;

   

总结:

本次主要学习的propertysynthesize要掌握其格式和作用.

0 0
原创粉丝点击