Katex

2012年9月8日 星期六

Mac Day 13 - Font Book

中文字型安裝

1. Font Book










2. 選擇字型:













3. 設定使用該字型



4. 設定後系統自動下載



5. 完成後即可選擇使用.

2012年9月7日 星期五

MAC Day 12 - Objective C - Class

和 C / C++ 不同地方

1. #import
   是當初 C: #include 的改良版, 保證標頭檔只會被匯入一次
   換言之, 標頭檔 就不用寫成像 :


#ifndef _XXX_H_
#define _XXX_H_
...
#endif

   而可以直接宣告


2.  @autoreleasepool

3.  物件

C++:


obj->method(argument);


Objective C:


[obj method: argument];



[ ClassOrInstance method ];

myDog = [Dog new];
[myDog run];


4. Interface

@interface NewClassName : ParentClassName
    propertyAndMethodDeclarations;
@end



@interface MyObject : NSObject 

+(return_type) class_method;            // 類方法
 
-(return_type) instance_method1;        // 實體方法
-(return_type) instance_method2: (int) p1;
-(return_type) instance_method3: (int) p1 andPar: (int) p2;

@end


+: 類似於 C++ : static function

5. Implementation

@implementation NewClassName
{
    memberDeclarations;
}
    methodDefinitions;
@end



@implementation MyObject
{
    int memberVar1; // 實體變數
    id  memberVar2;
}
+(return_type) class_method { .... //method implementation } -(return_type) instance_method1 { .... } -(return_type) instance_method2: (int) p1 { .... } -(return_type) instance_method3: (int) p1 andPar: (int) p2 { .... } @end


6. NSObject
http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html



7. program



@interface Dog: NSObject
-(void) print;
-(void) setName: (NSString*) name;
@end
@implementation Dog 
{
    NSString *name;
}
-(void) print {
    NSLog(@"%@", name);
}
-(void) setName: (NSString*) n {
    name = n;
}
@end
int main(int argc, const char * argv[])
{      
    @autoreleasepool {
        Dog *myDog;
        NSString *name = @"WonWon";
        
        myDog = [Dog new];
        
        [myDog setName: name];
        
        NSLog(@"My dog's name is");
        [myDog print];
        
    }
    return 0;
}





2012年9月6日 星期四

MAC Day 11 : objective-c : try catch


1. catch


@try {
    ...
}
@catch (CustomException *ce) {   // 1
    ...
}
@catch (NSException *ne) {       // 2
    // Perform processing necessary at this level.
    ...
 
}
@catch (id ue) {
    ...
}
@finally {                       // 3
    // Perform processing necessary whether an exception occurred or not.
    ...
}

2. throw


SException *exception = [NSException exceptionWithName: @"HotTeaException"
                                                 reason: @"The tea is too hot"
                                               userInfo: nil];
@throw exception;

2012年9月5日 星期三

MAC Day 10 - Objective-c - accessor method


1. .h : @property 宣告實體變數

@property(attribute1, attribute2…) type variable

writability :



readonly
-唯讀,只能讀取而不能設定值(不能用setXXXX的函式)。
readwrite-可讀可寫(預設)。


setter semantics


strong - 


f2 = f1;

[f1 retain];
[f2 release];
f2 = f1;

如果沒有將 f2 release, 可能會造成 memory leak
weak - 


避免 retain cycle. 
在 iOS 4 和 OS 10.6 不支援, 須改用 unsafe_unretained


copy-在設值時copy一份新資料,release舊資料。
assign-在設值時替換新舊資料, 而不會增加 retain count(預設)。
retain-在設值時retain新的資料,release舊資料。

Atomicity

nonatomic-預設為atomic以符合多工環境, 設成nonatomic可提高效率


e.g. 
@property double accumulator;



2. .m : @synthesize 存取實體變數

e.g.
@synthesize accumulator;



3. 則不需要額外函式, 就可以存取變數

e.g.
        [myCalc setAccumulator: 100.0];

        NSLog(@"The result is %g", [myCalc accumulator]);





2012年9月4日 星期二

Mac Day 9 - Objective-C - Xcode - Files


依類別存放檔案

1. File -> New -> File


2.  Objective-C class


3.  輸入 class 名稱


4. create 確定後會產生 .m 和 .h 檔




2012年9月3日 星期一

MAC Day 8 - Objective-C - NSString

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html


stringWithString:

Returns a string created by copying the characters from another given string.
+ (id)stringWithString:(NSString *)aString

e.g.     newStr = [NSString stringWithString: oldStr];

UTF8String

Returns a null-terminated UTF8 representation of the receiver.
- (const char *)UTF8String

e.g     NSLog(@"newStr: %s", [newStr UTF8String]);



熱門文章