LoginSignup
32
32

More than 5 years have passed since last update.

Xcode5.1+ Clang3.5 Tips

Posted at

Xcode5.1

Quick Lookの拡張

オブジェクトのコンテンツをデバック中に、Previewできる。

また、開発者は、QuickLookで閲覧したい内容を-debugQuickLookObjectで定義できる。

独自に-debugQuickLookObjectを実装する場合、以下の値を返す必要があります。(iOSのみ記載)

UIImage, UIImageView, CIImage, UIColor, UIBezirePath、CLLocation, UIView, NSString, NSAttributedString, NSData, NSURL

詳細は、こちら
Quick Look for Custom Types in the Xcode Debugger

Log breakpoint actionsでのログ出力

ブレイクポイントのEdit BreackPoint > Add Action > Log Messageで、NSLogのようにログ出力できるようになりました。

myString value: @myString@

記述すると、これまで、myStringのポインタ値が表示されていましたが、myStringの値が表示されるようになりました。

メリット

  • NSLogを製品コードに追加せず、デバックできる

デメリット

  • 設定までの操作が多く、少々面倒
  • どんなログ出力アクションを設定したのか、設定後に確認しづらい

うまい活用方法がありましたら、教えてください!

Xcode Snippetsの追加

Xcode5.1で、Xcode Snippetsが拡充されています。

Snippets Completion Shortcut Notes
-isEqual: and -hash methods isequal
-initXXXX: methods init Xcode5.1よりidinstancetypeになっている
+initialize methods initialize
Enumerated Type Declarations enumdef, nsenum, nsoption Fixed underlying Typeで列挙型の定義、NS_ENUM, NS_OPTIONS用のスニペット
-compare: method compare
-debugDescription, -description debugdescription, description return NSString stringWithFormat:...まで追加されるので、楽そう
Category, Extension @interface-category/extension
Inline Block as Variable inlineBlock ※Xcode5.0にもありますが、備忘録のため

Clang 3.5

Xcode 5.1でのDefault compilerは、 Apple LLVM compiler version 5.1 (clang-502)なので、Clang 3.5の機能が利用できるようです。

Boxed C String

@(<expression>)のBoxリテラルで、スカラー型よりNSNumberを生成できるのは有名ですが
char*や、const char *からNSStringも生成できます。

※エンコードがUTF8で、\0が終端の文字列を想定しています。

例えば、main.mで、以下を記述すると、int main(int,char*)の引数を出力することができます。

// Partition command line arguments into positional and option arguments.
NSMutableArray *args = [NSMutableArray new];
NSMutableDictionary *options = [NSMutableDictionary new];
while (--argc) {
    const char *arg = *++argv;
    if (strncmp(arg, "--", 2) == 0) {
        options[@(arg + 2)] = @(*++argv);   // --key value
    } else {
        [args addObject:@(arg)];            // positional argument
    }
}

quoted from Objective-C Literals Clang 3.5

他にも、xmlパーサーで、libxml2を利用しているケースなどで活用できそうです。

Attributes on Enumerators

以下のように、最新のバージョンで不良になった列挙値をdeprecatedとして、コンパイルに示すことができます。

typedef enum : NSInteger {
  Garlic,
  Salt,
  Wasabi __attribute__((deprecated)),
} PotatoChipType;

__attribute__((deprecated))は、DEPRECARED_ATTRIBUTEとしても記述できます。

TODO: deprecatedされている画像

Object Literals and Subscripting

※Clang 3.3より入っていた機能ですが、備忘録のため記載いたします。

NSArray, NSDictionary以外のカスタムオブジェクトで、[]リテラルを利用できます。

そのために、適時、以下のメソッドを適用します。

@interface CustomClass : NSObject
// customClass[idx] = obj;
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx; 

// id obj = customClass[idx];
- (id)objectAtIndexedSubscript:(NSUInteger)idx; 

// customClass[key] = obj;
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key; 

// id obj = customClass[key]
- (id)objectForKeyedSubscript:(id)key; 

ただ、このように独自クラスを生成できるものの、NSArray, NSDictionaryとの見分けが付きづらくなりますので、あまり利用しない方がよいでしょうね。

See Object Subscripting : NSHipster

その他

complex

C99より利用できるcomplex.hで、虚数を記述できるようになりましたが、それを簡単に記述できるリテラルがサポートされています。

#include <math.h>
#include <complex.h>
complex float x = { 1.0f, INFINITY }; // Init to (1, Inf)

quoted from Objective-C Literals Clang 3.5

lambdas(C++)とBlocks(Objective-C)の相互運用

Clang 3.5より、C++11よりlambdasのポインタをBlockへのポインタへ変換できるようになりました。

例えば、Objective-C++のコードで、以下のように記述できます。

NSArray *array = @[@"string 1", @"string 21", @"string 12", @"String 11",
                   @"String 02"];
const NSStringCompareOptions comparisonOptions
  = NSCaseInsensitiveSearch | NSNumericSearch |
    NSWidthInsensitiveSearch | NSForcedOrderingSearch;
NSLocale *currentLocale = [NSLocale currentLocale];
// See HERE
NSArray *sorted
  = [array sortedArrayUsingComparator:[=](id s1, id s2) -> NSComparisonResult {
             NSRange string1Range = NSMakeRange(0, [s1 length]);
             return [s1 compare:s2 options:comparisonOptions
             range:string1Range locale:currentLocale];
     }]; 
NSLog(@"sorted: %@", sorted);

quoted from Objective-C Literals Clang 3.5

CarPlay

CarPlay Simulator

まだまだ不明なことが多いCarPlayですが、以下の記事にあるような操作を行うと
iPhone SimulatorのHardWareとして、CarPlayを表示させることができます。

CarPlay を iOSシミュレータで試してみる

0xEDをバイナリ編集に利用していますが、もちろんvimでもできます。

※以下の操作を行う前に、必ずバックアップを取って下さい。

sudo vi -b libMobileGestalt.dylib

で対象のライブラリを開きます。xddコマンドで表示します。

:silent %!xxd -g 1
:set ft=xxd

00399daの4543へ変更します。

14750 00399d0: 8b 0d 82 66 01 00 84 c0 48 0f 43 0d 80 66 01 00  ...f....H.C..f..

最後に、バイナリ表示を解除して、保存します。

:%!xxd -r
:wq

CarPlay向けAPI

CarPlayで利用するAPIは、以下があるようですが、残念ながら
Appleからentitlementを取得しないと利用できないようです。

  • MPConnectionItem.h
  • MPPlayableContentManager.h
  • MPPlayableContentDelegate.h
  • MPPlayableContetnDataSource.h

上記は全て、MediaPlayer.frameworkなので
CarPlay対応予定とされる各インターネットラジオのサービスプロバイダー向けに
提供されているAPIなのでしょうね。

iOS8以降の展開に期待です!

32
32
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
32