Objective-C String Differences - objective-c

What's the difference between NSString *myString = #"Johnny Appleseed" versus NSString *myString = [NSString stringWithString: #"Johnny Appleseed"]?
Where's a good case to use either one?

The other answers here are correct. A case where you would use +stringWithString: is to obtain an immutable copy of a string which might be mutable.

In the first case, you are getting a pointer to a constant NSString. As long as your program runs, myString will be a valid pointer. In the second, you are creating an autoreleased NSString object with a constant string as a template. In that case, myString won't point to a real object anymore after the current run loop ends.
Edit: As many people have noted, the normal implementation of stringWithString: just returns a pointer to the constant string, so under normal circumstances, your two examples are exactly the same. There is a bit of a subtle difference in that Objective-C allows methods of a class to be replaced using categories and allows whole classes to be replaced with class_poseAs. In those cases, you might run into a non-default implementation of stringWithString:, which may have different semantics than you expect it to. Just because it happens to be that the default implementation does the same thing as a simple assignment doesn't mean that you should rely on subtle implementation-specific behaviour in your program - use the right case for the particular job you're trying to do.

Other than syntax and a very very minor difference in performance, nothing. The both produce the exact same pointer to the exact same object.
Use the first example. It's easier to read.

In practice, nothing. You wouldn't ever use the second form, really, unless you had some special reason to. And I can't think of any right now.
(See Carl's answer for the technical difference.)

Related

Modify strings with reflection

I was reading this question/answers, which basically showed an interesting behaviour in Java and strings, and two questions came up in my mind:
Are Objective-C/Swift String s behave the same? I mean if I have for example two variables which stores the same literal "someString", internally, will they refer to one "someString" object? I didn't find anything about it in the documentation.
If the answer to my previous question is yes, then is it possible to change same string literals the way like in Java?
Not all NSString literals (#"string literal") share the same storage due to compilation units.
NSString literals can not be changed in the program code, they are compiled into readonly segments.
NSString variables, that is that are created at runtime, only are shared by assignment.
NSString instances are immutable and can not be changed after creation.
NSMutableString instances can be modified and all variables pointing to such an instance point to the same change.
Swift is slightly different, as #Grimxn points out, Swift String is not a class and immutability is determined by the declaration syntax: let or var.

Objective-C Declaring Variables Within Loop -- Performance

I've been teaching myself Objective-C via some lectures from iTunes U.
I like the course, but the professor routinely writes code like this:
while (index < [self.textToAnalyze length]) {
NSRange range;
id value = [self.textToAnalyze attribute:attributeName atIndex:index effectiveRange:&range];
....
}
I have mostly worked in C and C++, and there are a couple of things about this code that seem glaringly wrong to me from a performance / style perspective.
The code declares two variables (of type NSRange and id) inside the loop. A naive compiler would reserve space for each of these types of variables at each iteration through the loop.
The code calls the length selector ([self.textToAnalyze length]) in the loop condition. As the programmer, I know that the length of the textToAnalyze property does not change while the loop is iterating. However, I'm not convinced that the compiler will know this. Won't this code call the length selector every single iteration of the loop?
I know that compilers can be very crafty, but I think it is bad code to declare variables within a loop and call functions within the loop conditions. It could affect the performance, and in my mind, it is certainly poor style.
However, I am new to Objective-C, so here is my question:
Is this bad code, or are these Objective-C idioms that are fine to use?
The answer is: it depends.
For variables in loops, moving them out of the loop might improve performance of the loop, but now you've changed the scope and effected performance in another way. Which is better is definitely going to be situation-based.
But, as far as I'm concerned, a variable with a scope any wider than necessary is bad practice. Variables with unnecessarily wide scope and lead to user error. The amount of performance improvement you might get here is not worth ANY amount of debugging that could've been avoided with a narrower variable scope.
As for calling methods in loop conditions, again, this depends.
I'm quite certain that the length method of NSString is quite optimized. The length of a string is not analyzed every time length is called, and especially not so for NSString (as opposed to NSMutableString. If we're talking about an immutable string, calling length is simply returning an NSInteger value stored within the class and performance will be fine.
If, however, we're talking about an immutable string, we need to call length every time if it's important that we're only within the length of the string.
It's a mutable string. Even if this loop doesn't modify the string, it can be modified else where by anyone that has a reference to it. If you're concerned about performance, make an immutable copy of the string and use that immutable copy in the loop.
[myString length]
According to this answer, for both mutable and immutable versions, there is a variable within the class that stores the string's length. For immutable strings, this is calculated when the string is created and never changed. For mutable strings, this variable is calculated and set each time the string changes. At the end of the day, when you call string, it's purely returning the value of an internally stored int value in the class and will not be any slower than storing this length in a separate variable before the loop and comparing against this.
I think it is theoretically possible for some loop conditions to be cached, but in practice that doesn't seem to happen — the length method will be called every time through the loop.
Does that make this bad code? Not necessarily. The overhead of a function call is not that huge. If this loop needs to be tight, then yes, caching the string's length is a great idea. But in many cases, it just doesn't make any measurable difference, so less code is better code.
As for declaring variables in a loop, that isn't problematic. There are two mainstream Objective-C compilers in existence, and neither has any trouble compiling that to efficient code. I doubt even POC has trouble with that. Muddling your variables' scope just to please a hypothetical awful compiler isn't good code — it's premature optimization. (Incidentally, the same holds true for C++ too.)

How do I avoid conversion of signs in a good practice way (without casting)?

I am taking an NSInteger (long int) from a method and using it as a parameter in a method, which takes an NSUInteger (unsigned int).
NSInteger input = [someObject someMethod];
[someOtherObject someOtherMethodWithNSUInteger: input];
This isn't a great idea. What if input is a negative number? How can I do this in a way that is a good practice? Sure, I could just go
[someOtherObject someOtherMethodWithNSUInteger: (NSUInteger)input];
But that's not a good solution because if input is negative, it will not bound it to zero. I could resort to it if there was no other way, but I would rather not.
Solutions that will not work:
Why can't I just make someMethod return an NSUInteger? Or make someOtherMethodWithNSUInteger: take an NSInteger? It's because I don't own them, they are a part of Cocoa Touch.
What about returning nil and using NSError or throwing an exception? The method that I am putting this code in conforms to a protocol, so I can't add an (NSError**) parameter to the method, and returning nil without an NSError explaining why seems like a bad idea because since I don't have access to someMethod's or someOtherMethod's source code, there would be nothing for me to even fix. And if I throw an exception, there is no way for me to catch it because this is code will be used by a closed class who made the protocol.
How can I fix this type conversion problem?
I think the answer somewhat straightforward, and it's basically what you said yourself.
If someOtherMethodWithNSUInteger expects unsigned and the only thing you have is a signed value (returned from someMethod) then two things will happen: (1) half of the possible values expected will never be used, and (2) half of the possible values returned are invalid. No matter what method you use you will always have these issues. So the easiest thing to do is use a simple type-casting, and trim negative values to prevent them being interpreted as very large positive values.
[someOtherObject someOtherMethodWithNSUInteger: (NSUInteger)MAX(input,0)];

To pointer or not to pointer? Or is this a pointer at all?

Alright, it's been a long time since I've worked with pointers. And now I've been writing .NET code for more than a decade so I haven't had to deal with them. In fact, in .NET it's really nice because if it's not a value type then it's clearly a reference type, or by definition a pointer.
So, in .NET when I declare a string it's most certainly a pointer underlying because it's a reference type:
string s = "Hello Mike!";
However, it appears that in Objective-C I can declare a string two different ways:
NSString* s = "Hello Mike!";
NSString s = "Hello Mike!";
Now, if I'm understanding this correctly the first declaration is very similar to the underlying declaration of a string in .NET, a pointer. But what exactly is the second?
Now bear in mind I may be way off base here because I'm just starting to dig into Objective-C, so please excuse my ignorance!
Neither of your NSString declarations should actually compile. The first should tell you that it's an incompatible assignment because your assigning a C string pointer as if it were an object address. The second should tell you that you can't actually declare an object, only a pointer to an object. The correct syntax is:
NSString* s = #"Hello Mike!";
In this case, s is a pointer variable which contains the address of a string object, having the "Hello Mike!" value.
Coming from REALbasic to Objective-C was similar for me: they both use pointers as references to object instances, but in REALbasic that fact is implicit, whereas Objective-C is C and therefore must make it explicit. That fact, however, is really just an accident of notation. The implications, for things like assignment and comparison, are similar.
You might be helped by reading the relevant entry from my book on this topic:
http://www.apeth.com/iOSBook/ch03.html#_an_instance_reference_is_a_pointer
(The fact that you don't seem to understand yet how to form an NSString literal (it starts with an at-sign, e.g. #"hello", is secondary.)

Should I use an intermediate temp variable when appending to an NSString?

This works -- it does compile -- but I just wanted to check if it would be considered good practice or something to be avoided?
NSString *fileName = #"image";
fileName = [fileName stringByAppendingString:#".png"];
NSLog(#"TEST : %#", fileName);
OUTPUT: TEST : image.png
Might be better written with a temporary variable:
NSString *fileName = #"image";
NSString *tempName;
tempName = [fileName stringByAppendingString:#".png"];
NSLog(#"TEST : %#", tempName);
just curious.
Internally, compilers will normally break your code up into a representation called "Single Static Assignment" where a given variable is only ever assigned one value and all statements are as simple as possible (compound elements are separated out into different lines). Your second example follows this approach.
Programmers do sometimes write like this. It is considered the clearest way of writing code since you can write all statements as basic tuples: A = B operator C. But it is normally considered too verbose for code that is "obvious", so it is an uncommon style (outside of situations where you're trying to make very cryptic code comprehensible).
Generally speaking, programmers will not be confused by your first example and it is considered acceptable where you don't need the original fileName again. However, many Obj-C programmers, encourage the following style:
NSString *fileName = [#"image" stringByAppendingString:#".png"];
NSLog(#"TEST : %#", fileName);
or even (depending on horizontal space on the line):
NSLog(#"TEST : %#", [#"image" stringByAppendingString:#".png"]);
i.e. if you only use a variable once, don't name it (just use it in place).
On a stylistic note though, if you were following the Single Static Assigment approach, you shouldn't use tempName as your variable name since it doesn't explain the role of the variable -- you'd instead use something like fileNameWithExtension. In a broader sense, I normally avoid using "temp" as a prefix since it is too easy to start naming everything "temp" (all local variables are temporary so it has little meaning).
The first line is declaring an NSString literal. It has storage that lasts the lifetime of the process, so doesn't need to be released.
The call to stringByAppendingString returns an autoreleased NSString. That should not be released either, but will last until it gets to the next autorelease pool drain.
So assigning the result of the the stringByAppendingString call back to the fileName pointer is perfectly fine in this case. In general, however, you should check what your object lifetimes are, and handle them accordingly (e.g. if fileName had been declared as a string that you own the memory to you would need to release it, so using a temp going to be necessary).
The other thing to check is if you're doing anything with fileName after this snippet - e.g. holding on to it in a instance variable - in which case your will need to retain it.
The difference is merely whether you still need the reference to the literal string or not. From the memory management POV and the object creational POV it really shouldn't matter. One thing to keep in mind though is that the second example makes it slightly easier when debugging. My preferred version would look like this:
NSString *fileName = #"image";
NSString *tempName = [fileName stringByAppendingString:#".png"];
NSLog(#"TEST : %#", tempName);
But in the end this is just a matter of preference.
I think you're right this is really down to preferred style.
Personally I like your first example, the codes not complicated and the first version is concise and easier on the eyes. Theres too much of the 'language' hiding what it's doing in the second example.
As noted memory management doesn't seem to be an issue in the examples.