-->

2014年4月30日水曜日

【マルチスレッド】Objective Cのマルチスレッド実装方法

ObjCのマルチスレッドについてちょっと詳しくなったからメモ

1. GCDを使う
 前にも書いたけど、スレッドはGCDを使うのが楽
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
    [self doSomething]; //別スレッド
    dispatch_sync(dispatch_get_main_queue(), ^{
        [self doSomething]; // メインスレッド
    });
});
この場合、別スレッドに重い処理を書いて、メインスレッドはUIの描画が一般的らしい
PRIORITYで優先度も設定できる
そして、インスタンスメソッドはスレッドを指定して実行することができる
[self performSelectorOnMainThread:@selector(refresh)
                        withObject:nil
                    waitUntilDone:NO];
2. NSOperationを使う
NSOperationQueue *queue = [NSOperationQueue new];
    MyOperation *operation = [MyOperation new];//NSOperationを継承したクラスを作成
    [queue addOperation:operation];
 
3. NSInvocationOperationを使う
 この場合、NSOperationの継承クラスを作らなくてもいい
NSInvocationOperation *invOperation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(aaa) object:nil];
    [queue addOperation:invOperation];
4. とりあえずバックグラウンドに投げる
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{


        // ここに非同期でバックグラウンドに投げたい処理を書く

    });

2014年4月29日火曜日

【UICollectionView】カスタムセルをxibで作成

storyboard上でUICollectionViewを用意して、
xibで作ったUICollectionViewCellのカスタムクラスを配置する

1. file > new > file > UserInterface でviewを選択、ファイル名をmyCell.xib
 ラベルをUICollectionViewCellの上に配置する
2. 同じ名前の.hと.mファイルを作成する
3. myCell.xibのFIle's ownerのCustom ClassをmyCellに

 viewを選択して、identifierをmyCellに
 
  Custom CalssもmyCellに

myCellにプロパティを記述
myCell.h
 
#import 

@interface myCell : UICollectionViewCell

@property (strong, nonatomic) IBOutlet UILabel *cellLabel;


@end
myCell.m
 
#import "myCell.h"

@implementation myCell


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


-(void)awakeFromNib
{
    NSLog(@"bundleLoader waked");
}

@end


4. myCell.xibのFIle's ownerではなく、viewを選択して
 inspectorのoutletから用意したプロパティとxib上のラベルをつなぐ

このやりかたでないと、
 Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x6858780> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key… 

とエラーが...

5. あとはcollectionViewController.mで
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *identifier = @"myCell";
    static BOOL nibCellLoaded = NO;
    
    if(!nibCellLoaded){
        UINib *nib = [UINib nibWithNibName:@"myCell" bundle:nil];
        [collectionView registerNib:nib forCellWithReuseIdentifier:identifier];
        nibCellLoaded = YES;
    }
    
    
    myCell *cell = (OBWeather*)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];

    return cell;
}
 
 

最後に思ったけど、
クラス名とidentifierって一緒じゃない方がいいのかなあ

【iOS】Podfileの設定とエラー

Podfileの記述例:
 
platform :ios, '7.0'
pod 'LXReorderableCollectionViewFlowLayout'
pod 'MagicalRecord' 


 [!]  Unable to satisfy the following requirements: 
 - `LXReorderableCollectionViewFlowLayout (= 4.3)` required by `Podfile` 
みたいなエラーが出る場合は、上記の例みたいにiOSのバージョンを指定する必要がある


 [!]  Oh no, an error occurred.
Search for existing github issues similar to yours:
ってエラーが出る場合はこれ

$ sudo rm -rf ~/.cocoapods/repos/master
$ sudo pod setup



[参考]http://qiita.com/u651601f/items/87203d22857d3634f955

【UICollectionView】セルの並び替えをする

iOSのホーム画面みたいに、UICollectionViewのセルを並び替えるライブラリ
http://urouro.net/blog/2014/01/17/lxreorderablecollectionviewflowlayout/

いつか使えるであろう...

2014年4月25日金曜日

【iOS】メソッドのラベルを省略

Obj-Cで、引数が複数あるメソッドは通常

 - (リターン型)メソッド名:(引数型1)引数名1 ラベル:(引数型2)引数名2 ラベル:(引数型3)引数名3...

と続くが、ラベルは省略可能(推奨はされてない?)ということを知りました。
省略すると

  - (リターン型)メソッド名:(引数型1)引数名1:(引数型2)引数名2:(引数型3)引数名3...

 となります。呼び出す時は、

  [インスタンス名 メソッド名:引数1:引数2:引数3];

すごい違和感

2014年4月23日水曜日

【iOS】マルチスレッド

マルチスレッドに手をつけたのでメモ
http://blog.77jp.net/dispatch_async-copy-paste
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // バックグランドでAPIなどを実行
    });
    
    dispatch_async(dispatch_get_main_queue(), ^{
        // メインスレッドで処理をしたい内容、UIを変更など。
    });
    
    // APIなどはバックグランドで実行して、UIはメインスレッドで処理をしたい場合。
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // バックグランドでAPIなどを実行
        dispatch_async(dispatch_get_main_queue(), ^{
            // メインスレッドで処理をしたい内容、UIを変更など。
        });
    });
ちなみに今回からSyntax Highlighterを導入してみました
コードが美しい