ios高級進階技能

有一種方便的方式來訪問插入到 storyboard 容器視圖的子控制器:

Objective-C
// 1. A property has the same name as a segue identifier in XIB
@property (nonatomic) ChildViewController1 *childController1;
@property (nonatomic) ChildViewController2 *childController2;

// #pragma mark - UIViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue
                 sender:(id)sender
{
    [super prepareForSegue:segue sender:sender];

    // 2. All known destination controllers assigned to properties
    if ([self respondsToSelector:NSSelectorFromString(segue.identifier)]) {
        [self setValue:segue.destinationViewController forKey:segue.identifier];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // 3. Controllers already available bc viewDidLoad is called after prepareForSegue
    self.childController1.view.backgroundColor = [UIColor redColor];
    self.childController2.view.backgroundColor = [UIColor blueColor];
}

NSDateFormatter +dateFormatFromTemplate:options:locale:

友情提示:如果你在使用 NSDateFormatter -setDateFormat: 而不同時使用NSDateFormatter +dateFormatFromTemplate:options:locale: 那麼你很可能做錯了。

文檔是這樣的:

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

不同的語言對時間要素有不同的規範。你用這個方法來得到某個特定語言(通常使用當前的語言 - 參看 currentLocale)給定的時間要素的正確字符串格式。


NSBundle -preferredLocalizations

有時候,你需要知道你的應用程序在什麼語言環境下運行。通常,人們會用 NSLocale +preferredLanguages。不幸的是這除了告訴你應用程序實際上顯示的語言之外一無所知。它只是給你 iOS 中 "Settings → General → Language & Region → Preferred Language" 或是 OS X 裏 "System Preferences → Language & Region → Preferred Languages" 同樣的有序列表。

想象一下如果首選語言順序是 {英語, 法語} 但是你的應用程序只支持德語。調用[NSLocale preferredLanguages] firstObject] 會返回英語而不是你想要的德語。

得到應用程序使用的準確語言環境的正確方式是使用 [[NSBundle mainBundle] preferredLocalizations]

文檔是這麼說的:

一個 NSString 對象的數組包含了 bundle 裏的區域語言 ID。這些字符串是按用戶系統設置和可用本地化來排序的。

NSBundle.h 裏的註釋說:

這個 bundle 本地化的子集,會對這個進程的當前執行環境的優先順序上重新排序;主 bundle 的首選本地化顯示了用戶是最有可能在 UI 看到的語言(文本)

你大概還需要使用 NSLocale +canonicalLanguageIdentifierFromString: 來確保規範的語言標識。

阻止 dylib 鉤子

來自 Sam Marshall 的這一招使黑客的生活更艱難:> >

把這一行加到你的 "Other Linker Flags" 裏:

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

揭露 CocoaPods!

有一個快速方法來檢查(閉源)應用程序使用的所有源:

$ class-dump -C Pods_ /Applications/Squire.app | grep -o "Pods_\w+"

不需要重新編譯的重新運行

如果你一遍又一遍的調試同樣的問題,你可以不重新編譯就運行你的應用程序: "Product > Perform Action > Run without Building" (⌘⌃R)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章