Objective-C 常用代碼記要

來源:互聯網
上載者:User

NSImage與NSData之間轉換

NSData *imageData = [NSData
dataWithContentsOfFile: imagePath];

   
UIImage *aimage = [UIImage
imageWithData: imageData];

   
//UIImage-> NSData

   
NSData *imageData = UIImagePNGRepresentation(aimae);

隨機數

srandom(time(NULL));

產生 5 ------20
之間的一個隨機數

int n =
5 + (int) (20.0 * (random() / (RAND_MAX +
1.0)));

srandrand官方已經不推薦使用。原因是產生隨機數的效能不是很好, 

另外是隨機數的隨機性沒有random好,
再者就是不是安全執行緒。

NSLog(@"%@",NSStringFromSelector(_cmd));

前往模擬器調試下的程式目錄

/private/var/root/Library/Application Support/iPhone Simulator/5.1/Applications

讀取序列化檔案

NSString *resourcePath = [[NSBundle
mainBundle] resourcePath];

NSString *path = [[NSString
alloc]initWithFormat:@"%@/president.plist",resourcePath];

NSData *data = [[NSData
alloc]initWithContentsOfFile:path];

President *thePres = [NSKeyedUnarchiver
unarchiveObjectWithData:data];

儲存序列化檔案

NSString *resourcePath = [[NSBundle
mainBundle] resourcePath];

   
NSString *path = [[NSString
alloc]initWithFormat:@"%@/president.plist",resourcePath];

   
NSData *data = [NSKeyedArchiver
archivedDataWithRootObject:self.president];

   
if([data
writeToFile:path atomically:NO])

    {

       
UIAlertView *alert = [[UIAlertView
alloc]initWithTitle:@"System"
message:@"儲存成功!" delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];

        [alert
show];

        [alert
release];

    }

訪問主控委託

AppDelegate *delegate = [[UIApplication
sharedApplication]delegate];

訪問主程式

   
UIApplication *app = [UIApplication
sharedApplication];

2)添加自訂的產生後指令碼

在Build Phases中添加一個Phase,右下角的Add
Build Phase,然後單擊Add Run Script,輸入以下指令碼

export CODESIGN_ALLOCATE=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate

if [ "${PLATFORM_NAME}" == "iphoneos" ]; then

/Developer/iphoneentitlements401/gen_entitlements.py "my.company.${PROJECT_NAME}" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/${PROJECT_NAME}.xcent";

codesign -f -s "iPhone Developer" --entitlements "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/${PROJECT_NAME}.xcent" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/"

fi

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.