objective c - NSString and NSMutableString methods and casting via mutableCopy -
coming c++ background, i'm amazed have write next code avoid warnings:
nsmutablestring *foobar = [@"foobar" mutablecopy]; // line 1 foobar = [[foobar capitalizedstring] mutablecopy]; // line 2
line 1 seems normal me: can't straight cast const string string.
line 2 puzzles me: capitalizedstring method belongs nsstring , returns nsstring. in order impact nsmutablestring must "recast" via mutablecopy.
but why isn't capitalizedstring overloaded in nsmutablestring "-(nsmutablestring*) capitalizedstring" ?
if case, write next no warnings:
foobar = [foobar capitalizedstring]; // line 2
ps: know write both lines in single one, example.
so in order impact nsmutablestring must "recast" via mutablecopy
mutablecopy
not "recast" in c++. creates mutable copy. there recasting in objc, , isn't it. distinction of import because "recast" suggests you're taking single object , treating different way. mutablecopy
creates new object, has memory management implications. (those handled arc, still of import understanding what's going on.)
but why isn't capitalizedstring overloaded in nsmutablestring "-(nsmutablestring*) capitalizedstring" ?
objective-c not have overloading. impossible, if desirable. whether desirable interesting question. in utilize cases more convenient. in other utilize cases less convenient (immutable strings have optimizations available mutable strings lose, if wanted optimizations, have re-create string yet 1 time again create immutable). it's moot; overloads not possible. have you're asking for, you'd have create -mutablecapitalizedstring
, have copy
, mutablecopy
. (that'd silly, because capitalizedstring
pretty rare.)
as @zaph points out, nsmutablestring
uncommon in objc. said, questions apply -sortedarrayusingselector:
, basic question valid.
it's worth noting swift have overloads, , you're describing done in swift (though break type inference, in cases annoying). overloads swift saw valuable add. objc doesn't have them.
objective-c nsstring
No comments:
Post a Comment