zoukankan      html  css  js  c++  java
  • 程序的执行

    1,从终端执行代码。

    -fobj  意思是编译OC语言。

    -arc  使用自动引用计数。

    -framework  链接到Foundation框架。

    -o 指定执行文件,文件会被创建。

    clang -fobjc-arc -framework Foundation main.m -o mappp

    别忘了加arc。

    编译的main.m代码。

    #import <Foundation/Foundation.h>

    int main (int argc, const char * argv[]){

            @autoreleasepool {

                    NSString *helloString = @"Hello World";

                    NSLog(@"%@", helloString);

            }

            return 0;

    }

    2,控制台打印,

    NSLog();注意类型。

    3,创建一个自定义类,比如Car.h

    #import <Foundation/Foundation.h>

    @interface Car : NSObject

    @end

    Car.m

    #import “Car.h"

    @implementation Car

    @end

    用的时候

    Car *car = [[Car alloc] init];

    4,在类中定义属性。

    #import <Foundation/Foundation.h>

    @interface Car : NSObject{

    @private

        NSString *name_;

    }

    @property(strong) NSString *name;

    @end

     

    readwrite 读写属性,需要getter和setter属性。

    readonly  只读属性,只需要getter。

    strong  强指针,保留内存。

    weak  弱引用,当对象为nil时,内存释放。

    assign 简单赋值,不更改索引计数。

    nonatomic 非原子性访问,对属性赋值的时候不加锁,多线程并发访问会提高性能。

    copy 建立一个索引计数为1的对象,然后释放旧对象对NSString

    retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的索引计数为1

    5,@synthesize

    比如你的Car.h

    #import <Foundation/Foundation.h>

    @interface Car : NSObject

    @property(strong) NSString *name;

    @end

     

    Car.m

    #import "Car.h"

    @implementation Car

    @synthesize name;

    @end

    你可以使用.或发送方法获取name值。

    car.name = @"Sports Car";

    NSLog(@"car is %@", car.name);

    你甚至可以设置name值,具体示例。

    代码示例。

     Car *car = [[Car alloc] init];

    car.name = @"Sports Car";

    NSLog(@"car.name is %@", car.name);

    [car setName:@"New Car Name"];

    NSLog(@"car.name is %@", [car name]);

    6,添加类方法。

    +(void)writeDescriptionToLogWithThisDate:(NSDate *)date;

    调用类方法....全是基础我就不细写了。

    [Car writeDescriptionToLogWithThisDate:[NSDate date]];

    类方法不用实例话

    7,实例方法。 太基础,不细写了。

    -(void)writeOutThisCarsState{

        NSLog(@"This car is a %@", self.name);

    }

            Car *newCar = [[Car alloc] init];

     

            newCar.name = @"My New Car";

     

            [newCar writeOutThisCarsState];

    8,扩展类

    Listing 1-13. HTMLTags.h

    #import <Foundation/Foundation.h>

    @interface NSString (HTMLTags)

    -(NSString *) encloseWithParagraphTags;

    @end

    Listing 1-14. HTMLTags.m #import "HTMLTags.h"

    @implementation NSString (HTMLTags)

    -(NSString *) encloseWithParagraphTags{

            return [NSString stringWithFormat:@"<p>%@</p>",self];

    }@end

    Listing 1-15. main.m

    #import "HTMLTags.h"

    int main (int argc, const char * argv[]){

            @autoreleasepool {

    NSString *webText = @"This is the first line of my blog post";

    //Print out the string like normal:

    NSLog(@"%@", webText);

    //Print out the string using the category function:

    NSLog(@"%@", [webText encloseWithParagraphTags]);

    }

     

    return 0; }

    注意下@interface NSString,然后用的时候[webText encloseWithParagraphTags],就是说给NSString添加了一个自己的方法。

     

  • 相关阅读:
    教你一招Linux下文本比对方法
    Linux下find与exec的联手干大事
    Linux下Shell日期的格式,你知道几种?
    Linux下Python3.6的安装及避坑指南
    多线程中使用CompletableFuture
    ElasticSearch7.6.2中语法使用(更新中)
    ElasticSearch7.6.2使用_update_by_query语法
    ElasticSearch7.6.2使用_delete_by_query产生版本冲突问题
    filebeat7.6.2进程运行一段时间后自动退出问题解决
    把本地项目提交到gitLab
  • 原文地址:https://www.cnblogs.com/guanliyang/p/3913412.html
Copyright © 2011-2022 走看看