Objective C why format string as string - ios

While working on project code left to me by a previous dev, I have encountered the following construct
-(NSString *)StringCheckWithString:(NSString *)string{
NSString *string2 = [NSString stringWithFormat:#"%#", string];
if([string2 length] == 0){
return #"none";
}
else {
return string2;
}
}
Can anyone explain why you would do this, it seems significantly overengineered to me and I don't understand why it has been done this way (for clarity, I don't understand why the string is formatted like this, I understand the length check)

The argument that is passed in could be any subclass of string, including NSMutableString. This code creates an immutable copy of it. This means that you can store the returned string without having to worry about someone else modifying it.
A better way of doing this would be:
NSString *string2 = [string copy];
According to the NSCopying Protocol reference:
The copy returned is immutable if the consideration “immutable vs.
mutable” applies to the receiving object.

Related

how to replace the content of NSMutablestring in objective c

I am new in objective c. I want to replace the content of mutable string I am using code as
NSMutableString *myMutableStringObj = [myMutableStringObj stringByReplacingOccurrencesOfString:#" & " withString:#"And"];
But It Shows me warning
Incomparable pointer types assigning to NSMutableString * from NSString *
Any Suggestion about this. I am using MutableString.
It tells you this function returns NSString *. If you want to clean that warning. Just do like this:
NSMutableString *myMutableStringObj = [[myMutableStringObj stringByReplacingOccurrencesOfString:#" & " withString:#"And"] mutableCopy];
See the method signeture from NSString Class.
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement
this method return NSString object.
So you need to convert result into mutalbe string or catch the result in NSString.
Read the documentation for NSString and NSMutableString. That doesn't only apply to you, but to anyone answering here so far. The answers on this thread have been absolutely depressing.
There are methods that can be used for NSString but also work with NSMutableString which create a new, immutable string based on a previous string. Typically methods starting with "stringBy..." will be creating a new string.
And there are methods, in the NSMutableString interface, which modify the contents of an existing NSMutableString. Like "replaceOccurencesOfString".
Read the documentation and pick what is right for you. A method starting with "stringBy" is not going to modify your mutable string, it's creating a new immutable one. To modify a string, use the methods from the NSMutableString interface. Fortunately the compiler tells you when you use the wrong method.
plz use this code
NSMutableString * str1 = [NSMutableString stringWithString:#"Hello Testing"];
str1 = [[str1 stringByReplacingOccurrencesOfString:#"T" withString:#"B"] mutableCopy];
NSLog(#"%#",str1);

isEqualToString returns false for identical strings?

Maybe I'm going nuts, but I can't seem to understand why isEqualToString is returning false in this case.
I have written the following method to decode strings from percent encoded URL:
-(NSString*) decodeFromURLSafe: (NSString*) urlToDecode{
NSString *safeUrl = [urlToDecode stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
safeUrl = [safeUrl stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
return safeUrl;
}
In practice it works, but I wrote some tests for it and don't understand why they are failing
Here's one such test:
-(void)testDecodingFromURLSafe{
NSString *unescapedString = #"#a!b*c(d)e;f:g&h=i+j$k,l/m#n\\p[q]r<s>t^u{v}w|x~y";
NSString *answer = [_viewController decodeFromURLSafe:#"%40a%21b%2Ac%28d%29e%3Bf%3Ag%26h%3Di%2Bj%24k%2Cl%2Fm%23n%5C%5Cp%5Bq%5Dr%3Cs%3Et%5Eu%7Bv%7Dw%7Cx%7Ey"];
NSLog(#"%#", answer);
NSLog(#"%d", [answer isEqualToString:#"#a!b*c(d)e;f:g&h=i+j$k,l/m#n\\p[q]r<s>t^u{v}w|x~y"]);
XCTAssert([answer isEqualToString:unescapedString], #"decoding works");
}
The string in the second NSLog statement is copied directly from the output of the first:
2014-11-20 14:03:46.352 ProjName[5573:60b] #a!b*c(d)e;f:g&h=i+j$k,l/m#n\\p[q]r<s>t^u{v}w|x~y
But both the comparisons return false and I'm confused as to what I'm missing.
I've tried putting the string into DiffNow to double check that they're identical and still nothing.
You need to double your backslashes, so that they escape correctly:
#a!b*c(d)e;f:g&h=i+j$k,l/m#n\\\\p[q]r<s>t^u{v}w|x~y

Multiple NSString declaration and initialization

I am new to the ios development.I was checking the NSString and where I had found out that it was excessed using the multiple ways which is as under
1) NSString *theString = #"hello";
2) NSString *string1 = [NSString stringWithString:#"This is"];
3) NSString *string =[[NSString alloc]init];
string =#"Hello";
Now I am confused with above three and would like to know the differences between then?
Yes all three are ways to create string...I try to explain you one by one.
One think you must remember that in Objective-c string is represented by #"".
whatever comes double quotes is string eg #"Hello" is string. #"" is basically literal to create NSString.
NSString *theString = #"hello";
EDIT:
In this statement we are creating an NSString constant. Objective-C string constant is created at compile time and exists throughout your program’s execution.
2. NSString *string1 = [NSString stringWithString:#"This is"];
In this statement again, we are creating an autorelease object of NSString, but here a slide diff. than the first case. here we are using another NSString object to create a new autorelease object of NSString. So generally we use stringWithString method when we already have NSString Object and we want another NSString object with similar content.
3. NSString *string =[[NSString alloc]init];
string =#"Hello";
Here, In first statement you are creating a NSString Object that you owned and it is your responsibility to release it (In non-ARC code), once you done with this object.
Second statement is similar to case 1, by string literal you are creating string Object.
but when you put these two statements together it causes you memory-leak(In non-ARC code), because in statement one you are allocating & initiating memory for new string object and right after in second statement you are again assigning a new string object to the same string reference.
1) and 2) is the same. there is no difference. [NSString stringWithString: str] just does nothing and returns str if str is immutable already. via this link
3) you create empty NSString pointer. after you assign this string with ' = ' is same way with 1) and it alloc memory to your NSString. You can watch this NSString pointer in 3) and you see pointer was changed:
NSString *string =[[NSString alloc]init];
printf("\n string_1-> = %p\n", string );
string = #"Hello";
printf("\n string_2-> = %p\n", string );
OK, first off, the syntax #"blah" is an Objective-C literal to create an NSString. Essentially it is an NSString.
Also, there are two parts to each statement. The declaration of the string variable NSString *string and the instantiation string = <something>.
So...
This is how you would do it most of the time. You declare the variable and set it using the objective-c literal.
This adds a bit of redundancy and you wouldn't really use it like this. You'd maybe use it with a substring or something from another string. As it is you are essentially creating two objects here. The first is [NSString stringWithString:<>]; and the second is #"This is".
This is a bit odd. You are first creating an empty string #"" and then you are replacing this string with #"Hello". Don't do this. There is no point. You may as well just do number 1 as the result is the same.
All three will work but in these exact cases you'd be better off using the first.

How to filter a string after a particular character in iOS? [duplicate]

This question already has answers here:
Split an NSString to access one particular piece
(7 answers)
Closed 9 years ago.
I want to filter string after character '='. For eg if 8+9=17 My output should be 17. I can filter character before '=' using NSScanner, how to do its reverse??? I need a efficient way to do this without using componentsSeparatedByString or creating an array
Everyone seems to like to use componentsSeparatedByString but it is quite inefficient when you just want one part of a string.
Try this:
NSString *str = #"8+9=17";
NSRange equalRange = [str rangeOfString:#"=" options:NSBackwardsSearch];
if (equalRange.location != NSNotFound) {
NSString *result = [str substringFromIndex:equalRange.location + equalRange.length];
NSLog(#"The result = %#", result);
} else {
NSLog(#"There is no = in the string");
}
Update:
Note - for this specific example, the difference in efficiencies is negligible if it is only being done once.
But in general, using componentsSeparatedByString: is going to scan the entire string looking for every occurrence of the delimiter. It then creates an array with all of the substrings. This is great when you need most of those substrings.
When you only need one part of a larger string, this is very wasteful. There is no need to scan the entire string. There is no need to create an array. There is no need to get all of the other substrings.
NSArray * array = [string componentsSeparatedByString:#"="];
if (array)
{
NSString * desiredString = (NSString *)[array lastObject]; //or whichever the index
}
else
{
NSLog(#""); //report error - = not found. Of array could somehow be not created.
}
NOTE:
Though this is very popular splitting solution, it is only worth trying whenever every substring separated by separator string is required. rmaddy's answer suggest better mechanism whenever the need is only to get small part of the string. Use that instead of this approach whenever only small part of the string is required.
Try to use this one
NSArray *arr = [string componentsSeparatedByString:#"="];
if (arr.count > 0)
{
NSString * firstString = [arr objectAtIndex:0];
NSString * secondString = [arr objectAtIndex:1];
NSLog(#"First String %#",firstString);
NSLog(#"Second String %#",secondString);
}
Output
First String 8+9
Second String 17
Use this:
NSString *a =#"4+6=10";
NSLog(#"%#",[a componentsSeparatedByString:#"="])
;
Log: Practice[7582:11303] (
"4+6",
10
)

How to better initialize strings to avoid dead stores

I found a few questions like this but I couldn't find this particular question. I have a number of strings that are initialized as
NSString *string = [[NSString alloc] init];
which are then assigned a value depending on the results of an if/else block:
if ([anotherThing isEqualToString:#"boogers"]) {
string = [NSString stringWithFormat:#"some characters"];
} else {
string = [NSString stringWithFormat:#"some _other_ characters"];
}
and then string is used later in the method.
How can I accomplish this without leaving a dead store at the alloc/init stage? If I alloc inside the if (or the else), the string is gone by the time I need it several lines down.
You don't have to initialize the string on that first line -- you just need to declare it:
NSString *string = nil;
if ([anotherThing isEqualToString:#"boogers"]) {
string = #"some characters";
} else {
string = #"some _other_ characters";
}
The [NSString stringWithFormat:] will initialize a new NSString object for you, basically what you are doing is declaring a new object each time, just set the pointers for your strings as NSSTring *someString, *someOtherString, *allTheStringsYouNeed; and then use any class method to initialize it even #"Characters"; will work correctly as the compiler do it at runtime for you.

Resources