macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

initWithBytes:objCType:

値オブジェクトを値とオブジェクティブCタイプで初期化して返します
-(id)initWithBytes:(const void *)value:
               objCType:(const char *)type:

解説

値オブジェクトを値とオブジェクティブCタイプで初期化して返します。
typeは@encode()コンパイラディレクティブで作成したものでなければなりません、C文字列でハードコーディングしてはいけません。
指定のイニシャライザです。selfを返します。
【オブジェクティブタイプ】
● @encode(NSRange) 範囲
● @encode(char *) C文字列

返り値

( id )

値オブジェクト

引数

( const void * )value

( const char * )type

オブジェクティブタイプ

クラス

NSValue

Instance Methods

使用可能

10.0

参照

例文

#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{

	//char*を渡す場合
	char *aCString = "thestring";
	NSValue *theValue = [NSValue valueWithBytes:&aCString objCType:@encode(char *)];
	char *bufferChar[100];
	[theValue getValue:&bufferChar];
	NSLog(@"---%s",*bufferChar);

	//単にポインタを渡す場合
	id anObject = @"Obj-C string";
	NSValue *theValueOfPointer = [NSValue valueWithBytes:&anObject objCType:@encode(void *)];
	id theObjPointer;
	[theValueOfPointer getValue:&theObjPointer];
	NSLog(@"---%@",theObjPointer);

	//上の例を書き換えるとこうなる
	NSValue *theValueOfObj = [NSValue valueWithNonretainedObject:@"Obj-C string"];
	id theObj;
	[theValueOfObj getValue:&theObj];
	NSLog(@"---%@",theObj);

}

@end