2015年11月25日水曜日

独自クラスのArrayを指定のコード順に並べ替える(メモ)

 EntityItem*item1 = [[EntityItem alloc]init];
    item1.name = @"なまえ1";
    item1.code = @"id10";

    EntityItem*item2 = [[EntityItem alloc]init];
    item2.name = @"なまえ2";
    item2.code = @"id03";
    EntityItem*item3 = [[EntityItem alloc]init];
    item3.name = @"なまえ3";
    item3.code = @"id12";
    EntityItem*item4 = [[EntityItem alloc]init];
    item4.name = @"なまえ4";
    item4.code = @"id05";
    EntityItem*item5 = [[EntityItem alloc]init];
    item5.name = @"なまえ5";
    item5.code = @"id02";
//並び替える独自クラス入り配列
    NSArray *origin = @[item1,item2,item3,item4,item5];
    
//並び替えたいコードの順番    
    NSArray *sort = @[@"id02",@"id10",@"id03",@"id12",@"id05"];

 //並び替え
    NSArray *sortedArray = [origin sortedArrayWithOptions:NSSortConcurrent
                                           usingComparator:(NSComparator)^(EntityItem* obj1,EntityItem* obj2){
                                               NSUInteger index1=[sort indexOfObject:obj1.code];
                                               NSUInteger index2=[sort indexOfObject:obj2.code];
                                               if(index1>index2){
                                                   return NSOrderedDescending;
                                               }else {
                                                   return NSOrderedAscending;
                                               }
                                           }];

//並び変わった確認
    for(EntityItem*item in sortedArray){
        NSLog(@"code:%@",item.code);
    }
    
   
 

2011年8月3日水曜日

ストリングからクラスとセレクター(メソッド)生成

クラスやメソッド(@selector())はNSArrayやNSDictionary格納できない。
でも、NSStringからセレクターとクラスを生成できるからそれ格納しておけばおっけーですね!

Aclassname = @"TestView";
Bselectorname = @"TestMethod";

Class Aclass = NSClassFromString(Aclassname);
SEL Bselector = NSSelectorFromString(Bselectorname);

2011年6月20日月曜日

#defineの改行

#defineが長くて一行で収まらん!って時は改行できます

#define TES(__OBJ1,__OBJ2) {\
 if(__OBJ1!= __OBJ2){\
[__OBJ1 release];\
__OBJ1 =[__OBJ2 retain];} }

\のあとは何も入力せずに次の行にいくこと。スペースも駄目。
ちなみに"\"の入力はmacの場合「alt+¥」です。

2011年6月16日木曜日

UITabBarControllerのselectedIndex

UITabBarControllerのプロパティ、selectedIndexは
選択中のタブ番号(左から0~)返してくれるやつなんですが、
タブ切り替えでこのselectedIndexが変わるタイミングが若干ずれることに気づいた。

タブ0→タブ1に切り替えた際のタブ1の
ビューコントローラーで走るビューサイクルの例です。
※自分が乗ってるタブバーのインスタンス(tabBar)はどうにかして取得すること前提

-(void)viewWillAppear:(BOOL)animated{
     [super viewWillAppear:animated];
     NSLog(@"nowTabIndex:%d",tabBar.selectedIndex);//この時点ではtab0
}

-(void)viewDidAppear:(BOOL)animated{
     [super viewDidAppear:animated];
     NSLog(@"nowTabIndex:%d",tabBar.selectedIndex);//この時点ではtab1
}

…タブ切り替わった瞬間にindex変わろうよ!!

ところでタブバーに乗ってるビューコンからタブバーのインスタンスって
どうやって取得するんだ…?ナビコンみたいにセルフに問い合わせればたどり着けるの?
ぬーんタブバーあまり扱わないから未知数です…。

デフォルトフォント

意外に知ってると便利なのでデフォルトシリーズは
ここに書き出していく予定。
ログ出したらこうなった。

UIButtonのデフォルトフォント(xcode上で作成の場合)
 font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 18px
 ※xibから作ったUIButtonのデフォルトフォント違うかもしれん

2011年6月1日水曜日

NSDictionaryの高速列挙

allValuesをつけると直接オブジェクトを取り出せるので覚えておくと便利かも。
そのかわりキーわからんけども。

-(void)firstEnum:(NSDictionary *)dict{
     for(NSString * keyname in dict){
            NSLog(@"キーは%@",keyname);
     }

     for(id obj in [dict allValues]){
            NSLog(@"オブジェクトは%@",obj);
     }
}

2011年5月28日土曜日

例外処理とAutoreleasePool

例外処理内で作ったAutoreleasePoolは@fainallyで解放しなくていいらしい

-(NSMutableArray *)testes{
 NSMutableArray * returnArr = [NSMutableArray array];
     @try{
       for(int Cnt= 0;Cnt<100;Cnt++){
           NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
       //[returnArr addObject:@"なにか"];  
            NSLog(@"%d番目は%@",Cnt,[returnArr objectAtindex:Cnt]);//←例外発生
            [pool release];//ここにはこない
       }
     }@catch (NSException * e) {
           NSLog(@"%@",e);
     }@finally {
     //pool解放なし
     }
     return returnArr;
}
…逆に今までfinallyに書いてたからクラッシュしてたのかしらー。

参考 NSAutoreleasePool祭り