- (id)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)length encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)flag
“bytes” is not a const pointer. Does it mean this method may modify “bytes”?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You should assume that it may mutate since that is the signature and there is not documentation that otherwise negates that.
So my answer is: It is because the
SEL‘s signature does not/cannot change forNSMutableStrings that it is declared non-const.Here are some
NSStringcreation alternatives which suggest a little more safety without copy:CFStringCreateWithBytesNoCopyCFStringCreateWithCStringNoCopyCFStringCreateWithCharactersNoCopyChances are good that the
NSStringAPIs in question also use these.CF-APIs provide more appropriate functions for mutable variants, e.g.
CFStringCreateMutableWithExternalCharactersNoCopy, and these are likely whatNSMutableStringwould use.