IOS: NSString split - ios

I have this NSString:
NSString *perc = #"- 20 %";
I want only "20 %".
What can I do?

[perc subStringFromIndex:2];
There are different functions for substrings, take a look at them either typing "subString" and getting xcode help, or in traditional way - from doc :)

Related

Is that integer equal to the number in textfield

I'm new to Xcode and Objective-C. I watched a tutorial where he compared a char with the entered text in the textfield of the simulator. He used the following code:
BOOL isUsersEqual = [self.username isEqualToString:[self.usernameTextField text]];
When trying to build a small app myself I have problems to get the number out of the Textfield and be processed into my code. I want it to compare the random number I generated to the number in the textfield. How can I accomplish that?
I know this is kind of a nooby question and I don't wanna ask every time. Is there someplace I can look up all those types of questions?
You can try it by converting the number into string and then comparing -
NSString *number = [self.username stringValue];
if(number isEqualToString:self.usernameTextField.text){
//Do Something
}
else{
//Do Something
}
But this method only works when you're comparing Equals To -
For Greater than/ Less than/Equal to you can try it by converting the textfield's text into number and then comparing -
NSNumber *number = [self.usernameTextField.text integerValue];
if(number == self.username){
//Do Something
}
else{
//Do Something
}
To compare your generated number with a number from textField, you must first typecast the textField text to NSInteger using
NSInteger b = [[textField text] integerValue];
then you can proceed to compare the 2 numbers.
To fully understand these types of questions, I recommend you learning about objective-c primitive types and typecasting. Good luck!

"ԅ" causing iOS app crash

I find that if I set UILabel's text to "ԅ", it crashes without further information. It didn't hit the "All Exception" breakpoint.
self.label.text = #"ԅԅ";
How do I get rid of this crash? Thank you in advance!
By the way, I found this problem due to an emoticon "ԅ(¯﹃¯ԅ)", which kind of describes what I'm feeling right now.
Do not pass directly a Unicode character to NSString initialisation, instead use the character's Unicode code-point representation. In case of this character, it'd be #"\u504".
try this:
NSData *strd = [text dataUsingEncoding:NSUTF16StringEncoding];
NSString *newStr = [[NSString alloc] initWithData:strd encoding:NSUTF16StringEncoding];

Memory optimization when assigning NSString to UILabel text

In my view, I have a UILabel which text property is updated every second.
- (void)updateLabelWithValue:(NSInteger)value
{
self.label.text = [NSString stringWithFormat:#"%i", value];
}
Isn't this costly memory wise? It creates a new NSString every time. Is there a way to just have one NSString instance and update its text?
Thanks
You're worrying too much about a very very minor optimization. Yes, you're creating a new instance of NSString on every pass, but you're also destroying the old one, so in essence you're replacing the string.

iOS: Column formatting for NSString not working in UITextView?

I'm working on an iPad app and I need to be able to format some output on the screen in a columnar format. This was similar to my question:
How can I use the \t [tab] operator to format a NSString in columns?
and I used that solution, unfortunately I'm not seeing the same results. My code is as follows:
NSString* titleColumn = [[NSString stringWithFormat:#"%#", displayName] stringByPaddingToLength:25 withString:#" " startingAtIndex:0];
NSString* serializedValue = [[NSMutableString alloc] init];
NSString* valueAsString = [NSString stringWithFormat:#"%.03f", value];
serializedValue = [serializedValue stringByAppendingFormat:#"%#%#", titleColumn, valueAsString];
When logging the items in the console, they are properly aligned, however, when plugging them into a UITextView, they are misaligned. This is how I'm sticking them in the text view:
NSMutableString* displayString = [[NSMutableString alloc] initWithString:#""];
for (NSString* entry in textToDisplay)
{
NSLog(#"%#", entry);
[displayString appendString:entry];
[displayString appendString:#"\n"];
}
[self.fTextPanel setText:displayString];
Any ideas on what I'm doing wrong?
EDIT:
In the log, it looks like this:
Inclination 0.000
Version 0.000
Inferior/Superior 0.000
Anterior/Posterior 0.500
And the UITextView version looks like this: http://imgur.com/vrUzybP,OYxGVd8
The reason for misaligned is the different width for each character. The best way for displaying this type of information is by using UITableView, You can use title and subTitle field for displaying or you can design a custom UITableView.
the reason is because when drawing charachters (glymphs), each character will have different widths,i.e character small c and big C will take different widths for drawing , so it wont work. Even though there are two strings with equal lengths but different characters, the drawing text will have different widths.
What i would suggest you for this is to have two textViews side by side, render titles in one textVIew and values in the next TextView.
If the number of titles is large and if you enable scrolling in textVIews, use delegate Methods of UIScrollView, and when scrollHappens in either of them, set the contentOffset of the other to make it look like single TextView.
Hope this helps.

Replace lines of text in iOS

I am needing to replace a line of text in code on my iOS app, however the lines that need to be replaced in the particular NSString will be different for different entries on the XML that is being parsed.
For example, I need to replace 129727-the-cave.mp3 with 129727.jpg.
However, I can't just tell it to replace -the-cave.mp3, because some instances of the string will have a different number and title of mp3. I think the next line is:
129838-my-song.mp3.
So, basically, I need a way to find everything from the first hyphen through mp3 and replace it, no matter what the text is?
Try this:
NSString *original = #"129727-the-cave.mp3";
NSString *sub = [original substringToIndex:[original rangeOfString:#"-"].location];
NSString *finalString = [sub stringByAppendingString:#".jpg"];
NSLog(#"finalString: %#", finalString);

Resources