Trouble with ID in NSString - ios

I am trying to create something interesting in my application. So, I created an UILabel and I want to output new value.
So, my code.
NSString *test = #"13";
self.UserAge.text = #"Your age is %#", test;
But it doesn't work.
In Console-Command Mode I can do it with NSLog();
My result is "Your age is %#". But I need to output "Your age is 13". What do I should do with name?
Sorry, if my question is easy for you. I am beginner. :)
Thank you everyone who will answer on my question.

You want:
NSString *test = #"13";
self.UserAge.text = [NSString stringWithFormat:#"Your age is %#", test];
Your version is equivalent to:
NSString *test = #"13";
self.UserAge.text = test;
And I would suggest you use the correct data type, which for an age is an integer:
NSUInteger age = 13;
self.UserAge.text = [NSString stringWithFormat:#"Your age is %ld", age];
// this might be %d, depending on platform ^^^

You need to do the following:
NSString *test = #"13";
self.UserAge.text = [NSString stringWithFormat:#"Your age is %#", test];

Related

How to show new line with numbers format from server response of String in iOS

I am getting server response as "We've organizations in following something locations. 1. First test 2. Second test 3. Third test 4. Fourth test. Choose one for more information";
I am working on Objective-C and trying to showing in UILabel, but issue is I want to show as Paragraph. Like splitting the string by following.
We've organizations in following something locations.
First test
Second test
Third test
Fourth test
Choose one for more information.
The above one is example, but the data is completely dynamic and no idea about how many points would be there in response string.
Anyone have idea about this.
Try this :
NSString *str = #"We've organizations in following something locations. 1. First test 2. Second test 3. Third test 4. Fourth test.";
str = [str stringByReplacingOccurrencesOfString:#"1." withString:#"\n 1."];
str = [str stringByReplacingOccurrencesOfString:#"2" withString:#"\n 2"];
str = [str stringByReplacingOccurrencesOfString:#"3" withString:#"\n 3"];
str = [str stringByReplacingOccurrencesOfString:#"4" withString:#"\n 4"];
yourlbl.text = str;
Dynamic way
NSString *str = #"We've organizations in following something locations. 1. First test 2. Second test 3. Third test 4. Fourth test.";
NSCharacterSet *numberCharset = [NSCharacterSet characterSetWithCharactersInString:#"0123456789"];
NSScanner *theScanner = [NSScanner scannerWithString:str];
while (![theScanner isAtEnd]) {
// Eat non-digits and negative sign
[theScanner scanUpToCharactersFromSet:numberCharset
intoString:NULL];
int aInt;
if ([theScanner scanInt:&aInt]) {
str = [str stringByReplacingOccurrencesOfString:[NSString stringWithFormat:#"%d",aInt] withString:[NSString stringWithFormat:#"\n %d",aInt]];
}
}
You can use like this
NSArray *array = [NSArray arrayWithObjects:#"First test",#"Second test",#"Third test" , #"Fourth test Choose one for more information.", nil];
__block NSString *strConcate = [NSString new];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
strConcate = [strConcate stringByAppendingString:[NSString stringWithFormat:#"%lu. %#\n",(unsigned long)(idx + 1),obj]];
}];
self.labelString.text = strConcate;
Finally, I found the solution, here it is. This may helps someone in future.
NSArray *pointsArray = [trimmedString componentsSeparatedByString:#"."];
NSString *strOccurence, *strReplacing;
for (int i=1; i<pointsArray.count; i++){
strOccurence = [NSString stringWithFormat:#"%d.",i];
strReplacing = [NSString stringWithFormat:#"\n%d.",i];
trimmedString = [trimmedString stringByReplacingOccurrencesOfString:strOccurence withString:strReplacing];
}
cell.textLabel.text = trimmedString;

Create NSString with different memory

I created three string, that bind it data with method argument. However, i face issue that all of three string share same memory, therefore, it show the same text. Here is how i create it:
-(void)buildViewsWIithTitle:(NSString*)eventTitle{
NSString *firstStr = #"";
NSString *secondStr = #"";
NSString *thirdStr = #"";
Next i set label text to all of this three string. I set it value like:
thirdStr = [NSString stringWithFormat:#"%#", eventTitle];
secondStr = [NSString stringWithFormat:#"%#", eventTitle]
firstStr = [NSString stringWithFormat:#"%#", eventTitle];
In console i output its memory just after creation:
NSLog(#"memory %p , %p , %p", firstStr, secondStr, thirdStr);
memory 0x109af82d8 , 0x109af82d8 , 0x109af82d8
Any idea how make memory address different for them?
As you are using NSString for all 3 objects, which can't be mutated, The compiler will check the value of the string, which is the same empty to all strings. The compiler will optimise the memory usage by pointing the same memory.
The iOS compiler optimizes references to string objects that have the same value (i.e., it reuses them rather than allocating identical string objects redundantly), so all three pointers are in fact pointing to same address.
If you used the NSMutableString, still it may point to the same object, but when you try to mutate the string, it will be copied to the new memory(Lazy memory allocation).
if you want the different memory for each string then you can allocate the memory then initialize the string, like
NSMutableString *str1 = [[NSMutableString alloc]initWithString:#""]
NSMutableString *str2 = [[NSMutableString alloc]initWithString:#""]
NSMutableString *str3 = [[NSMutableString alloc]initWithString:#""]
But note that NSMutableString is mutable.
That's because your all string have address of [NSString stringWithFormat:#"%#", eventTitle]; and this is same for all strings.
For example if you will write below code in your viewdidload then you will get different memory address,
NSString *firstStr = #"";
NSString *secondStr = #"";
NSString *thirdStr = #"";
thirdStr = [NSString stringWithFormat:#"%#", #"eventTitle"];
secondStr = [NSString stringWithFormat:#"%#", #"eventTitle"];
firstStr = [NSString stringWithFormat:#"%#", #"eventTitle"];
NSLog(#"memory %p , %p , %p", firstStr, secondStr, thirdStr);
Because everystring has own memory and not pointing to any single string.
Maybe there's some kind of compiler optimisation going on. It determines (correctly) that the strings are the same so it optimises while compiling. As soon as you change your code so the strings will not be the same and recompile then the compiler won't optimise those particular strings.
thirdStr = [NSString stringWithFormat:#"a %#", eventTitle];
secondStr = [NSString stringWithFormat:#"b %#", eventTitle];
firstStr = [NSString stringWithFormat:#"c %#", eventTitle];

iOS: Create String from Variable and string combined

I need to concatenate a string and a variable together - I kind of find lots of examples of adding a string prior to a variable but not the other way round - how do I do this?
NSString *theImage = [[self.detailItem valueForKey:#" resimagefiletitle"] description], #"'add on the end";
Something like this:
NSString *theImage = [NSString stringWithFormat:#"%# %#",[self.detailItem valueForKey:#" resimagefiletitle"], #"'add on the end"];
Or:
NSString *theImage = [NSString stringWithFormat:#"%# add on the end",[self.detailItem valueForKey:#" resimagefiletitle"]];
Try this
NSString *theImage = [NSString stringWithFormat:#"%# '>",[[self.detailItem valueForKey:#"resimagefiletitle"] description]];
Here I am considering [[self.detailItem valueForKey:#"resimagefiletitle"] description] gives NSString
We can concat diffrent type of datatypes into string by mention the format for it.
like if your want to concat two or more strings together then you can use the following code:
NSString *NewString = [NSString stringWithFormat:#"%#%#",#"This Is",#"way to concate string"];
and if your want concat integer value then you can mention the data format for it "%i".
eg:
int OutOf = 150;
NSString *NewString = [NSString stringWithFormat:#"%#%i",#"I got 100 out of ",OutOf];
this may help you.

combining two nsstrings on rss reader example

I am using something like this example as my base if you want to look at the code:
Advanced RSS Reader
And i am working with GTMNSString+XML and GTMNSString+HTML.
my problem is in RSSItem.m.
it already includes GTMNSString+HTML.I added GTMNSString+XML.
if i use gtm_stringBySanitizingAndEscapingForXML it does what it needs to do
NSString* description = [NSString stringWithFormat:#"%#...", [self.description substringToIndex:100]];
description = [description gtm_stringBySanitizingAndEscapingForXML];
If i do gtm_stringByUnescapingFromHTML it also does it.
NSString* description = [NSString stringWithFormat:#"%#...", [self.description substringToIndex:100]];
description = [description gtm_stringByUnescapingFromHTML];
What i want to do is combine it so that it goes through both gtm_stringByUnescapingFromHTML and gtm_stringBySanitizingAndEscapingForXML. I tried to do this but it didn't work:
description = [[description gtm_stringBySanitizingAndEscapingForXML] gtm_stringByUnescapingFromHTML];
Try like this:-
NSString* description1 = [NSString stringWithFormat:#"%#...", [self.description substringToIndex:100]]; description = [description gtm_stringBySanitizingAndEscapingForXML];
NSString* description2 = [NSString stringWithFormat:#"%#...", [self.description substringToIndex:100]];
description = [description gtm_stringByUnescapingFromHTML];
NSString* finalString=[description1 stringByAppendingString:description2];
NSLog(#"%#",finalString);

IOS Array Testing

Here is my current code:
int i = 1;
NSString * StockOneYahooFinance = [NSString stringWithFormat:#"http://finance.yahoo.com/q/hp?s=S+Historical+Prices"];
NSString * PulledStockOne = [NSString stringWithContentsOfURL:[NSURL URLWithString:StockOneYahooFinance] encoding:1 error:nil];
for (i=1;i=30;i++){
NSString *StartPulling = [[PulledStockOne componentsSeparatedByString:#"nowrap align="] objectAtIndex:i];
NSString *StartOpen = [[StartPulling componentsSeparatedByString:#">"] objectAtIndex:3];
NSString *Open = [[StartOpen componentsSeparatedByString:#"<"] objectAtIndex:0];
NSString *StartClose = [[StartPulling componentsSeparatedByString:#">"] objectAtIndex:9];
NSString *Close = [[StartClose componentsSeparatedByString:#"<"] objectAtIndex:0];
year.text = Close;
i++;
}
But to the point I click the only button on the screen and it does exactly what I want it pulls the stocks open and close price for the day. But my current issue is I want it to pull all of these as an array so how can I do this?
First thing:
for (i=1;i=30;i++){
it should be:
for (i=1;i<=30;i++){
Second one, do not increment 'int i' value on the end of loop because 'for' loop already do this. For quick'n dirty way of debugging add:
NSLog(#"Current iteration: %i", i);
as the first function in 'for' loop to see what's happening there.

Resources