2016年12月15日 星期四

OC筆記

OC筆記

Array

NSArray *Path = @[
                            @"/root/AppleInternal/Diags/Logs/Bonfire/",
                            @"/root/var/logs/Earthbound/",
                            @"/root/var/mobile/Media/FactoryLogs/LogCollector/Earthbound/"
                ];
//array調用
NSLog(@"%@",Path[1]);

class的宣告(位於.h頭文件)

@interface 類名 : NSObject{
@protected

}
@end

//example
@interface ASStudent : NSObject{
@protected //用protected聲明的變數,無法透過 . or -> 調用,只能夠過類所定義的方法調用。提升系統安全性。
    NSString *name; //宣告於NSString的方法都是實例方法都要使用指標。
    int age;
    NSString *sid;
}
-(NSString*) name;
-(void*) setName:(NSString*)aName;
-(int)age;
-(void)setAge:(int)aAge;
@end

class的實現(位於.m源文件)

@implementation
@end

@implementation
-(NSString*)name{
    return name;
}
-(NSString)setName:
@end

OC 函數宣告
OC 函數調用

字串處理

NSString *str = [[NSString alloc] initWithFormat:(nonnull NSString *), ...] //

int a = 5;
int b = 6;
NSString *str = [[NSString alloc] initWithFormat:@"%d + %d = %d",a,b,a+b];

[NSString stringWithString:(nonnull NSString *)], 用一個字串物件來建立另一個字串物件。返回一个auto-released的指向NSString的指针,所以不需要手工释放。
[NSString stringWithFormat:(nonnull NSString *), ...],

讀txt文件

函數stringWithContentsOfFile, 可將檔案讀近來變成字串。

NSString *txtPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/KernelPanicLog/SN.txt"];
NSString *SNData=[NSString stringWithContentsOfFile:txtPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",SNData);

創建資料夾

-(void)createDirForImage :(NSString *)dirName
{
    NSString *path;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:dirName];
    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:path])    //Does directory already exist?
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:path
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&error])
        {
            NSLog(@"Create directory error: %@", error);
        }
    }
    
}

沒有留言:

張貼留言

[LeetCode] Two sum (easy)

twosum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each...