iOS 開發中的一些注意點(安全、當前語言、時間格式化)


1.重復運行項目,不重復構建項目(來自Heath Borders

假如你一直在不停地調試同一個問題,你可以在不重復構建的情況下運行你的APP,這樣:“Product>Perform Action>Run without Building” 

2.禁用dylib鈎子(來自Sam Marshall

在你的“Other Linker Flags”里加上下面這行:

-Wl,-sectcreate,__RESTRICT,__restrict,/dev/null

3.NSBundle -preferredLocalizations

某些時候,你需要知道APP當前使用的是什么語言。eg:優先語言列表中只有{英語,法語},但你的APP僅使用德語;調用[[NSLocal preferredLanguages] firstObject]返回給你的是英語,而不是德語。正確的方法是用[[NSBundle mainBundle] preferredLocalizations]方法。

4.NSDateFormatter +dateFormatFromTemplate:options:locale:

友情提示:假如你調用[NSDateFormatter setDateFormat],而沒有調用[NSDateFormatter dateFormatFromTemplate:options:local:],n那么很可能出錯。

+ (NSString *)dateFormatFromTemplate:(NSString *)template
                             options:(NSUInteger)opts
                              locale:(NSLocale *)locale

 

不同地區有不同的日期格式。使用這個方法的目的:得到指定地區指定日期字段的一個合適的格式(通常你可以通過currentLocal查看當前所屬地區)

下面這個例子給我們表現了英式英語和美式英語不同的日期格式:

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
 
NSString *dateFormat;
NSString *dateComponents = @"yMMMMd";
 
dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:usLocale];
NSLog(@"Date format for %@: %@",
    [usLocale displayNameForKey:NSLocaleIdentifier value:[usLocale localeIdentifier]], dateFormat);
 
dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:gbLocale];
NSLog(@"Date format for %@: %@",
    [gbLocale displayNameForKey:NSLocaleIdentifier value:[gbLocale localeIdentifier]], dateFormat);
 
// Output:
// Date format for English (United States): MMMM d, y
// Date format for English (United Kingdom): d MMMM y

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM