ȥå   Խ ʬ Хåå ź ʣ ̾ѹ   ñ측 ǽ   إ   ǽRSS

Objective-C/API

Last-modified: 2011-11-14 () 01:04:16 (2139d)

Objective-C

ʸ(NSString)

NSString *str1 = [[NSString alloc] init];
ʸǽȥ󥿤Ȥ̴ΤϵˤʤOK
NSString *str1 = @"";
Ʊ
NSString *str1 = [[NSString alloc] initWithString:@"abc"];
ʸǽȥ󥿤Ȥ̴ΤϵˤʤOK
NSString *str1 = @"abc";
Ʊ
NSString *str1 = [NSString stringWithString:@"abc"];
ӥ˥󥹥󥹥ȥ饯ȥ󥿤Ȥ̴ΤϵˤʤOK
NSString *str1 = [NSString stringWithFormat:@"abc"];
եޥåդΥӥ˥󥹥󥹥ȥ饯㣱ȥ꡼סϿƤΤDzϵˤʤOKreleaseDzƤOK
NSString *str1 = [NSString stringWithFormat:@"i: %d", i];
եޥåդΥӥ˥󥹥󥹥ȥ饯㣲ȥ꡼סϿƤΤDzϵˤʤOKreleaseDzƤOK

ʹߤǤϲˤĤƾάƤޤ

ʹߤǤϡNSString *str1 = פʬάƤޤNSStringʸѤǡʸμФʤɤ⿷ʥ󥹥󥹤졢̤λѿǼꤹɬפޤʸʳ֤᥽åɤˤĤƤ˲֤ҤƤޤ

[str1 stringByAppendingString:str2]
ʸϢԤޤ
[str1 stringByAppendingFormat:@"i: %d", i]
եޥåȤʸϢԤޤ
[str1 length]
ʸΥХȿ֤ޤ
[str1 description]
self֤ޤ
[str1 isEqualToString:str2]
ʸ󤬰פ뤫ӤޤפYES֤ޤ
[str1 compare:str2]
Ӥޤ̤NSOrderedAscending, NSOrderedSame, NSOrderedDescendingΤɤ줫Ǥ
[str1 caseInsensitiveCompare:str2]
ʸʸ̤˼Ӥޤ̤NSOrderedAscending, NSOrderedSame, NSOrderedDescendingΤɤ줫Ǥ

ӤѲǽoptions:ˤĤƤϸͽ

Ф

[str1 substringWithRange:NSMakeRange(5, 15)]
ꤷʸ֤ĹʸФޤ
[str1 substringToIndex:5]
Ƭꤷʸ֤ޤǤʸФޤʸ֤ʸϴޤߤޤ
[str1 substringFromIndex:5]
ꤷʸ֤ޤǤʸФޤʸ֤ʸޤߤޤ
lineRangeForRangesubstringWithRangeѤԼФ
ʸڤȤƣԤļФޤ
int total_length = [str1 length];
NSRange range = NSMakeRange(0, total_length);
while (range.location < total_length) {
	range = [str1 lineRangeForRange:NSMakeRange(range.location, 0)]; // ϳϰ֤飱ԤϰϤ
	NSLog([string substringWithRange:range]); // ʸФѤޤʬŬؤޤ
	range.location = NSMaxRange(range); // ϳϰ֤򹹿
}
͡lineRangeForRange:length̾0ꤷޤʸꤹȡʬޤʹߤβԤǶڤޤ

ʸ

ɽ
\r, 0x0dCR, Mac OSβʸ
\n, 0x0aLF, Unixβʸ
\r\nCRLF, Windowsβʸ
U+2028Unicodeζڤʸ
U+2029UnicodeΥѥ饰նڤʸ

[str1 rangeOfString:str2]
ǽ˸Ĥä֤NSRange¤Τ֤ޤĤʤȤϹ¤ΤlocationNSNotFound֤ޤ

ִ

[str1 stringByReplacingCharactersInRange:range withString:str2]
ꤷϰϤʸִʸ֤ޤ
[str1 stringByReplacingOccurrencesOfString:str_to_find withString:str2]
ꤷʸ򤹤٤ִʸ֤ޤ

ʤɸͽ

Ѵʸ

Ѵ
[@"123" intValue]12310ʿʸint
[@"123" longLongValue]12310ʿʸlong long
[@"1.23" floatValue]1.23floatʸfloat
[@"1.23" doubleValue]1.23doubleʸdouble
[@"YES" boolValue]YESBOOLʸBOOL ͡ʸȸͤɤʳƬʸY, y, N, nȽǡ@"001", @"Yeh!"϶YESȤʤ
[@"123" integerValue]NSInteger12310ʿʸNSInteger

ʸѴ

ǽʸɰ

NSASCIIStringEncoding7bit ASCII
NSUTF8StringEncodingUnicode(UTF-8)
NSShiftJISStringEncodingܸShift JISʸ
NSISO2022JPStringEncodingŻҥ᡼ʤɤǻȤJIS
NSJapaneseEUCStringEncodingܸUNIXƥɸʸ
NSMacOSRomanStringEncodingMac OSΥ󥳡
NSString *str1 = [[NSString alloc] initWithCString:"abc" encoding:NSUTF8StringEncoding];
Cʸޤ
NSString *str1 = [NSString stringWithCString:"abc" encoding:NSUTF8StringEncoding];
Ʊʥӥ˥󥹥󥹥ȥ饯
char *cstr1 = [str1 cStringUsingEncoding:NSUTF8StringEncoding];
Cʸޤʥӥ˥󥹥󥹥ȥ饯
int byte_count = [str1 lengthOfBytesUsingEncoding:NSShiftJISStringEncoding]
ꤷ󥳡ǥ󥰤ȤɬפʥХȿ֤ޤΥ̥ʸϴޤߤޤ

initWithData:encoding:, dataUsingencoding, canBeConvertedToEncodingˤĤƤϸͽꡣ

webϤѴ

URL󥳡ɤʸ֤ȤϥץΤ褦˵Ҥޤ
stringByAddingPercentEscapesUsingEncoding:᥽åɤϡ(020)"#%><[\]^`{|}פפ$&+,/:;=?@פ򥨥פʤ褦ǤΤ褦ˤΤФΤ褦Ǥ

NSString* encodeURIComponent(NSString* s) {
	return [((NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
		(CFStringRef)s,
		NULL,
		(CFStringRef)@"!*'();:@&=+$,/?%#[]",
		kCFStringEncodingUTF8)) autorelease];
}

¾Ѵ

[str1 lowercaseString]
ʸִʸ֤ޤ
[str1 uppercaseString]
ʸִʸ֤ޤ
[str1 capitalizedString]
ñƬʸִʸ֤ޤ

ѥ

ĥҤpathExtensionʤ¿Υ᥽åɤͭޤܺ٤ˤĤƤϸͽ

ե

ͽ propertyList᥽åɤѤץѥƥꥹȥե뤫Υǡɤ߽Фϼ򻲾ȤƤ http://blog.syuhari.jp/archives/1723

ץѥƥꥹȥե롧XMLƱͤʵҤΥե롣ĥҤplistXcodeԽǽ

NSMutableString

ѹǽʸǤ󥹥ƤľѹޤNSStringΥ֥饹ǤΤǡNSStringΥ᥽åɤȤޤ

NSMutableString *mstr = [NSMutbleString stringWithCapacity:1000];
̤ꤷޤʥӥ˥󥹥󥹥ȥ饯ˡɬפʻdzĥޤȥ꡼סϿƤΤDzϵˤʤOKreleaseDzƤOKӥ˥󥹤ǤʤinitWithCapacity:ͭޤȥ꡼סϿƤȲ˺ʤΤǤȤɤǤ٥Ƚȥȥ꡼סϳΤǡ٥ȽޤľretainפʤȤreleaseɬפޤ

᥽åɤ֤ΤvoidǤ󥹥Ƥľѹ뤿ᡢκ¦ˡѿ = פȼƤϤޤʰʹߤƱ͡ˡ

[mstr setString:@"abc"];
ꤷʸꤷޤʥԡˡ

ɲ

[mstr appendString:str2];
ɲäޤ
[mstr appendFormat:@"i: %d", i];
˥եޥåʸɲäޤ

[mstr insertString:str2 atIndex:5];
ꤷʸ֤ޤ

ִ

[mstr replaceCharactersInRange:range withString:str2];
ꤷϰϤʸִʸ֤ޤ
int replace_count = [mstr replaceOccurrencesOfString:str_to_find withString:str2];
ꤷʸ򤹤٤ִޤִ֤ޤ Ѳǽoptions:range:ˤĤƤϸͽ

[mstr deleteCharactersInRange:NSMakeRange(5,15)];
ꤷϰϤʸޤ