u16suzuの blog

日々学んだことのメモブログです。

objc

正規表現による検索

// 正規表現オブジェクトを作成 NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"reg_exp" options:NSRegularExpressionCaseInsensitive error:nil]; NSString *targetString = @"target string" // 検索実…

定数にstructを使う

ヘッダファイル extern const struct LocalNotificationAttributes { __unsafe_unretained NSString *weeklyWork; } LocalNotificationAttributes; mファイル const struct LocalNotificationAttributes LocalNotification = { .weeklyWork = @"LocalNotific…

NSAssertでアサーションを行う

以下のようなコードを書くと int val = 299; NSAssert((0 <= val) && (val <= 255), @"Value is out of range!"); こんな感じでログが出て強制終了する. 2013-11-20 16:49:12.779 AppDemo[1935:60b] *** Assertion failure in -[HomeViewController viewDidL…

UIAppearanceを使ってボタンの背景画像を一括で設定する

ボタンがadd subviewされる側のクラスによって指定ができるけど, ちょっと使いづらい. カスタムなボタンを作った方がよいかもしれない. UIImage *btnImg = [UIImage imageNamed:@"hoge"]; UIImage *btnImgHighlighted = [UIImage imageNamed:@"fuga"]; [[UIB…

乱数を生成する

int r = arc4random_uniform(3); // range is 0~2 参考: http://programming-ios.com/objective_c-arc4random/

ナビゲーションバーとステータスバーを除いた画面の高さを取得する

ナビゲーションバーとステータスバーを除いた画面の高さを取得する. float backButtonYorigin = [[UIScreen mainScreen] applicationFrame].size.height - self.navigationController.navigationBar.height; コントロールの位置指定を 下からの距離で指定す…

複数の非同期処理や非同期処理内で場合分けする場合に便利な Deferred について

Deferredとは? 元ネタはjsからきた. 既存では非同期処理において完了,未完了の場合ごとにBlock実行をするというアイディアだったが そうはせずに, 完了,未完了の状態を持ったDefferedオブジェクトを即座に返すと言う方針. Defferedオブジェクトは完了もしく…

nullチェックをする

たまに補完で出てきてくれないときがあるのでメモ NSNumber *hoge; [hoge isEqual:[NSNull null]]

定数を static const で定義する

kは定数のプレフィックス. // .m file #import "ScrollButtonsView.h" static const int kButtonNumber = 5; static const float kButtonPadding = 10.0f; @implementation ScrollButtonsView /* 省略 */ @end 文字列定数の場合は以下の通り // .h file #imp…

Block propertyを使用してエラー処理を共通化する

実装ファイル #import "ViewController.h" #import "CustomView.h" typedef void(^testBlockType)(void); @interface ViewController () @property (nonatomic, copy) testBlockType success; @property (nonatomic, copy) testBlockType failuer; @end @imp…

カスタムビューの動作をビューコントローラ側で設定する

iQONのView構成紹介 14ページ目 http://www.slideshare.net/ararajp/vasilyretty?ref=http://tech.vasily.jp/2013/09/ios_iqon_view/ のスパイクプロジェクトをつくってみた. カスタムビューを作ったときにカスタムビューのなかではなく それを使うビューコ…

typedef を使って Blockの型を独自に定義する

typedef を使って Blockの型を独自に定義することができる. View.h #import <UIKit/UIKit.h> typedef void(^MyCustomBlockType)(void); @interface CustomView : UIView @property (nonatomic, copy) MyCustomBlockType blockName; @property (nonatomic, copy) void (^bloc</uikit/uikit.h>…

Objective-Cのデバッグ用ログ出力で 行数と関数名を表示する

以下を pchファイルに書けばOKです. // Debug #ifdef DEBUG #define DLOG(format, ...) NSLog((@"%s: %d: " format), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__) #else #define DLOG(format, ...) #endif pod にしたい... たしか, pod install したとき…

typedef with enum

typedef NS_ENUM(NSInteger, HogeState) { HogeStateStart, HogeStatePause, HogeStateEnd };

[ios] Objective-C の BOOL型は int型でどのように定義されているのか

Objective-C の BOOL型はint型で以下のように定義されている. YES: 1 NO: 0 objc.h ヘッダファイルにもそのように定義されている. 忘れたら BOOLの定義を ctrl+gで見れば良い. objc.hでの定義内容を以下に示す. /// Type to represent a boolean value. type…

ブロック変数を定義する

NSString* (^test)(NSString*, NSString*) = ^(NSString* head, NSString* tail){ return [NSString stringWithFormat:@"%@%@", head, tail]; }; NSLog(@"%@", test(@"hoge", @"fuga"));