其他

  • CGRect函式:存取CGRectxywidth、或height時,一律使用CGGeometry函式,而不要直接存取結構成員。

    CGRect frame = self.view.frame;
    
    // 使用CGGeometry
    CGFloat x = CGRectGetMinX(frame);
    CGFloat y = CGRectGetMinY(frame);
    CGFloat width = CGRectGetWidth(frame);
    CGFloat height = CGRectGetHeight(frame);
  • 列舉型別(Enumerated Types):使用enum時,建議使用擁有底層固定型別的新型宣告,因為可得到更佳的型別檢查與程式碼補齊功能;SDK裡含有巨集NS_ENUM(),方便我們加以運用。

    typedef NS_ENUM(NSInteger, NYTAdRequestState) {
        NYTAdRequestStateInactive,
        NYTAdRequestStateLoading
    };
  • 單件設計模式(Singletons):建立共享的單件物件時,應使用執行緒安全的寫法。

    + (instancetype)sharedInstance {
        static id sharedInstance = nil;
        static dispatch_once_t onceToken;
    
        dispatch_once(&onceToken, ^{
            sharedInstance = [[self alloc] init];
        });
    
        return sharedInstance;
    }

Last updated

Was this helpful?