Template

「Template」の編集履歴(バックアップ)一覧に戻る

Template - (2018/02/01 (木) 11:53:46) のソース

ViewController *__weak weakSelf = self;
typeof(UITableViewCell) *__weak wcell = [tableView cellForRowAtIndexPath:indexPath];

typeof(self) __weak wself = self;
__weak typeof(self) wself = self;



SomeClass *obj = [[SomeClass] alloc] init];
SomeClass *obj = [SomeClass new];
SomeClass *obj = SompeClass.new;



NSDictionary* dict = @{}
NSMutableDictionary* dict = @{}.mutableCopy;

NSArray* array = @[]
NSMutableArray* array = @[].mutableCopy;


+ (instancetype)sharedInstance
{
 SomeClass* obj = [[self alloc] init];
  :
  :
}

SomeClass* obj = self.new;


.h
extern const NSInteger BLGClassAIntegerConstant;
extern NSString *const BLGClassAStringConstant;

.m
const NSInteger BLGClassAIntegerConstant = 3;
NSString *const BLGClassAStringConstant = @"hogePublic";

static const NSInteger kLocalIntegerConstant = 30;
static NSString *const kLocalStringConstant = @"fooLocal";


NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ 
Delegate 
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ 
<protocol>

@protocol ClassNameDelegate <NSObject>

@optional
/**
 *  xxxxxxxxxxxxxxxx
 *  @param 無し
 *  @return 無し
 */
- (void)dismissSampleView;

/**
 xxxxxxxxxxxxxxxx
 @param sampleView SampleView インスタンス
 @param info  SampleView から通知したい情報
 @returns 無し
 デリゲートメソッドの命名として、引数が2つ以上ある場合は、第1引数に自身のインスタンス(今回はSampleViewControllerインスタンス)を渡す慣習があります。
 */
- (void)sampleView:(SampleView *)sampleView willDismissWithInfo:(NSDictionary *)info; 

@end

<property>
@property (weak, nonatomic) id <ClassNameDelegate> delegate;


_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Multi Thread
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
__weak typeof(self) wself = self;
typeof(self) __weak wself = self; 

// 非同期
dispatch_async(dispatch_get_main_queue(), ^{


});


// 同期
dispatch_sync(dispatch_get_main_queue(), ^{


});


// Main thread から dispatch_sync で main queue に突っ込もうとすると誰も処理できずに詰まるので注意。
// どの thread から呼ばれるかわからない場合は自分がどの thread にいるかチェックする。
if ([NSThread isMainThread]) {
    // 処理
} else {
    dispatch_sync (
        dispatch_get_main_queue(),
        ^{
            // 処理
        }
    );
}




dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{


  @synchronized(self) {


          
  }
  
  dispatch_async(dispatch_get_main_queue(), ^{


      
 });
});


_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ 
遅延 
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ 
#include <dispatch/dispatch.h> 

typeof(self) __weak wself = self;

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.3f * NSEC_PER_SEC); 
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) { 

     
     
}); 

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ 
サンプルアプリ作成用
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ 

#import "ViewController.h"

ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = viewController;



_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ 
typedef
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ 

typedef NS_OPTIONS(NSUInteger, UIXxxxxYyyyyZzzzz) {
  UIXxxxxYyyyyZzzzz00000      = 0,            // 00000
   UIXxxxxYyyyyZzzzz11111      = 1 << 0,       // 11111
   UIXxxxxYyyyyZzzzz22222      = 1 << 1,       // 22222
   UIXxxxxYyyyyZzzzz33333      = 1 << 2,       // 33333
   UIXxxxxYyyyyZzzzz44444      = 0x00FF0000,   // 44444
   UIXxxxxYyyyyZzzzzReserved   = 0xFF000000    // 予約(未使用)
};


typedef NS_ENUM(NSInteger, XxxxxYyyyyZzzzzType) {
    XxxxxYyyyyZzzzzType0    = 0,
    XxxxxYyyyyZzzzzType1,
    XxxxxYyyyyZzzzzType2,
    XxxxxYyyyyZzzzzTypeReserved     // 予約(未使用)
};


/**
 *  グラデーションを設定する。
 *  @param view 設定対象UIView
 *  @return 無し
 */
- (void)setGradientLayer:(UIView *)view
{
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = view.bounds;
    gradient.colors = @[
                        (id)[UIColor colorWithRed:0.90f green:0.91f blue:0.92f alpha:1.0f].CGColor,
                        (id)[UIColor colorWithRed:0.84f green:0.85f blue:0.86f alpha:1.0f].CGColor
                        ];
    [view.layer insertSublayer:gradient atIndex:0];
}

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
UITableView
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

.h
----------
@property (strong, nonatomic) NSMutableArray *tableItems;


.m viewDidLoad
-----------
- (void)viewDidLoad
{
  // ノーマルの場合
    NSString *className = NSStringFromClass([UITableViewCell class]);
   [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:className];
    
   
   // nib(カスタマイズ)の場合
    NSString *className = NSStringFromClass([CustomTableViewCell class]);
    [_tableView registerNib:[UINib nibWithNibName:className bundle:nil] forCellReuseIdentifier:className];   
}


.m UITableViewDelegate
-----------
#pragma mark - UITableViewDataSource
/**
 セクション数を返す
 @param tableView UITableViewインスタンス
 @return セクション数
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

/**
 セクションの要素数を返す
 @param tableView UITableViewインスタンス
 @param section セクション
 @return 要素数
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableItems count];
}

/**
 セルの高さを返す
 @param tableView UITableViewインスタンス
 @param indexPath NSIndexPathインスタンス
 @return せるの高さ
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

/**
 セルを返す
 @param tableView UITableViewインスタンス
 @param indexPath NSIndexPathインスタンス
 @return セルインスンタス
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *className = NSStringFromClass([UITableViewCell class]);
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:className
                                                            forIndexPath:indexPath];
    
    cell.textLabel.font = [UIFont systemFontOfSize:18];
    cell.textLabel.minimumScaleFactor = 9.f/18.f;
    cell.textLabel.adjustsFontSizeToFitWidth = YES;
    
    cell.textLabel.text = _tableItems[indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate
/**
 セルを選択すると呼ばれる
 @param tableView UITableViewインスタンス
 @param indexPath NSIndexPathインスタンス
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    
}

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ 
UICollectionView 
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ 

.h 
----------------------------------------------------------------------------- 
@property (strong, nonatomic) NSMutableArray *collectionItems; 

.m viewdidload 
----------------------------------------------------------------------------- 
NSString *className = NSStringFromClass([CustomCollectionViewCell class]);
[_collectionView registerNib:[UINib nibWithNibName:className bundle:nil] forCellWithReuseIdentifier:className]; 


.m delegate 
----------------------------------------------------------------------------- 
#pragma mark - UICollectionViewDataSource
/**
 *  セクション数を返す
 *  @param collectionView 対象UICollectionView
 *  @return セクション数
 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


/**
 *  要素数を返す
 *  @param collectionView 対象UICollectionView
 *  @return 要素数
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_collectionItems count];
}


/**
 *  表示対象セルを返す
 *  @param collectionView 対象UICollectionView
 *  @param indexPath インデックス情報
 *  @return 表示対象セル
 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *className = NSStringFromClass([UICollectionViewCell class]);
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:className
                                                                           forIndexPath:indexPath];
    NSDictionary *data = _collectionItems[indexPath.row];
    [cell setData:data];
    
    return cell;
}


#pragma mark - UICollectionViewDelegate
/**
 *  セル選択時に呼ばれる
 *  @param collectionView 対象UICollectionView
 *  @param indexPath インデックス情報
 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{ 



}