2012年3月8日 星期四

[Xcode] 如何在 XCode 4.2 設定部分程式碼不使用 ARC 方式分享

相信很多人已經開始在使用 XCode 4.2 與 iOS5 的功能了吧!其中我最喜歡的功能之一就是 ARC (Automatic Reference Counting)。簡單的來說,ARC 就是可以讓你以後不用再擔心那邊要 release,哪裡又得 retain 了 ;而由編譯器在編譯的時候自動幫你偷偷加上去,所以你的程式碼裡面也不可以有 release,retain等。這個功能可以說是讓人又愛又恨。愛的是從此可以擺脫 Memory leak 的問題了,恨的是有很多第三方的 Library 幾乎都還沒有轉換爲與 ARC 相容的程式碼。

    當然你也可以自己改,不過有時候看到一大堆紅字,心都涼了一半了,還改嘞!給XCode幫你改嘛,有時候又直接跟你說它改不了,自己手動改唄。所以很多人一定很希望部分程式碼使用 ARC 但部分程式碼則維持原來的方式,也就是可以在程式碼內使用 release,retain 等方法的呼叫。真有這麼便宜的事嗎?還真有嘞!不過在 XCode 4.2 中並沒有一個很明確的地方可以設定,所以知道的人也不多,好在谷歌兄什麼鬼東西都找的到,試用過後覺得很不錯用,特與大家分享如下:

1. 選擇專案,此時會出現專案設定畫面。
2. 選擇你的 Target,並切換到 Build Phases 畫面。
3. 找到 Compile Sources 這個畫面,然後選擇你要設定不使用 ARC 的程式碼
4. 按下 Enter 鍵後,會跳出一個視窗要你輸入東西,在裏面輸入 -fno-objc-arc 就可以了。


from http://dev.iphonetw.net/thread-6385-1-1.html

2012年2月23日 星期四

Sort NSFileManager Results

Ref

Sorting strings like Finder

The following example shows how you can compare strings to order them in the same way as they’re presented in Finder. First, define a sorting function that includes the relevant comparison options (for efficiency, pass the user's locale as the context—this way it's only looked up once).
int finderSortWithLocale(id string1, id string2, void *locale)
{
    static NSStringCompareOptions comparisonOptions =
        NSCaseInsensitiveSearch | NSNumericSearch |
        NSWidthInsensitiveSearch | NSForcedOrderingSearch;
 
    NSRange string1Range = NSMakeRange(0, [string1 length]);
 
    return [string1 compare:string2
                    options:comparisonOptions
                    range:string1Range
                    locale:(NSLocale *)locale];
}

2011年12月21日 星期三

偵測iOS還剩下多少記憶體的程式碼(iOS - detect total available/free disk space on the iPhone/iPad device)


-(float)getFreeDiskspace {
float totalSpace = 0.0f;
float totalFreeSpace = 0.0f;
NSError *error = nil;  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];  
if (dictionary) {  NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];  NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
totalSpace = [fileSystemSizeInBytes floatValue];
totalFreeSpace = [freeFileSystemSizeInBytes floatValue];
NSLog(@"Memory Capacity of %f MiB with %f MiB Free memory available.", ((totalSpace/1024.0f)/1024.0f), ((totalFreeSpace/1024.0f)/1024.0f));
} else {  NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);  }  
return totalFreeSpace;
}

2011年11月1日 星期二

用zlib來解壓縮資料


//uncompress data
- (void)uncompress:(char*)compressData uncompressData:(char*)uncompressData compressSize:(uint32_t)compressSize uncompressSize:(uint32_t) uncompressSize
{
  
    uLongf ld = uncompressSize;
    uLong sl = compressSize;
  
    int _ret = uncompress((Bytef*)uncompressData, &ld,(const Bytef *)compressData, sl);
    if( _ret == Z_OK)
    {
        DEBUG(@"Z_OK");
    }
}

2011年10月12日 星期三

char * <-> NSData


char* to NSData
[NSData dataWithBytes:myImageData length:myImageDataLength]
NSData to char*
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);