IF Else method, inside a NSString - ios

I have this NSString with format:
comandoInsert = [NSString stringWithFormat:#"INSERT INTO fazerbem_clients (image) Values('%#')", list[0]];
but most times the array list can be empty, causing a problem in the compiler, so I would like if possible to add a method if else within it, in a way that if the array is empty there he puts an empty string as #""
I tried this way just does not work:
comandoInsert = [NSString stringWithFormat:#"INSERT INTO fazerbem_clients (image) Values('%#')", [list[0] : #""] ];
How I can do this?

try this
comandoInsert = [NSString stringWithFormat:#"INSERT INTO fazerbem_clients (image) Values('%#')", list.count ? list[0] : #"" ];

Related

get simple value from string array in objective c

My code is like...
NSString *str=[arrInitiatives valueForKey:#"FileName"];
NSLog(#"file name ----> %#",str);
NSString *imageUrl = [NSString stringWithFormat:#"%#%#", PUBLIC_UTILITY_FORMS_URL,str];
NSLog(#"----> %#",imageUrl);
[_image_imageview setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:#"noimage.png"]];
//http://setupolice.org/brcsetu/UploadFile/Lighthouse4908.jpg
from this code,i will get this
file name ----> (
"Lighthouse4908.jpg"
)
----> http://setupolice.org/brcsetu/UploadFile/(
"Lighthouse4908.jpg"
)
I want this
----> http://setupolice.org/brcsetu/UploadFile/Lighthouse4908.jpg
Try this
NSString *str=[[arrInitiatives valueForKey:#"FileName"] objectAtIndex:0];
Regards,
Amit
When you create the str variable with code
NSString *str=[arrInitiatives valueForKey:#"FileName"];
In fact it doesn't return NSString object but NSArray object. That's why filename is inside parenthesis in log:
file name ----> (
"Lighthouse4908.jpg"
)
If you're sure that filename will always be present in the array you can try what #Amit Kalghatgi suggested in his answer.
The problem is that when this array is empty you will get an error during execution of this code. I would rather do something like this:
NSArray *filenames = [arrInitiatives valueForKey:#"FileName"];
NSString *str = nil;
if (filenames.count) {
str =[arrInitiatives valueForKey:#"FileName"];
}
NSLog(#"file name ----> %#",str);
Of course before creating imageUrl you'll have to check if str is nil or not.

present NSString as a line of elements

I have an NSString that hold data (actually that could be presented an NSArray). and i want to output that on a label.
In NSLog my NSString output is:
(
"cristian_camino",
"daddu_02",
"_ukendt_babe_",
"imurtaza.zoeb"
)
What i want is, to present it like :"cristian_camino","daddu_02","_ukendt_babe_","imurtaza.zoeb"
In a single line.
I could accomplish that turning string to an array and do following: arrayObjectAtIndex.0, arrayObjectAtIndex.1, arrayObjectAtIndex.2, arrayObjectAtIndex.3.
But thats look not good, and that objects may be nil, so i prefer NSString to hold data.
So, how could i write it in a single lane?
UPDATE:
There is the method i want to use to set text for UILabel:
-(void)setLikeLabelText:(UILabel*)label{
//Likes
NSString* likersCount = [self.photosDictionary valueForKeyPath:#"likes.count"];
NSString* likersRecent = [self.photosDictionary valueForKeyPath:#"likes.data.username"];
NSString *textString = [NSString stringWithFormat:#"%# - amount of people like it, recent "likes": %#", likersCount, likersRecent];
label.text = textString;
NSLog(#"text String is %#", textString);
}
valueForKeyPath: returns an NSArray, not an NSString. Whilst you've declared likersCount and likersRecent as instances of NSString, they're actually both arrays of values. You should be able to do something like the following to construct a string:
NSArray* likersRecent = [self.photosDictionary valueForKeyPath:#"likes.data.username"];
NSString *joined = [likersRecent componentsJoinedByString:#"\", \""];
NSString *result = [NSString stringWithFormat:#"\"%#\"", joined];
NSLog(#"Result: %#", result);
componentsJoinedByString: will join the elements of the array with ", ", and then the stringWithFormat call will add a " at the beginning and end.
The statement is incorrect, the internal quote marks (" that you want to display) need to be escaped:
NSString *textString = [NSString stringWithFormat:#"%# - amount of people like it, recent \"likes\": %#", likersCount, likersRecent];
If somebody curious how i fix it, there it is:
for (int i =0; i < [likersRecent count]; i++){
stringOfLikers = [stringOfLikers stringByAppendingString:[NSString stringWithFormat:#" %#", [likersRecent objectAtIndex:i]]];
}
Not using commas or dots though.

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.

Unknown escape sequence '\x20'

I've created this code
NSString *insertSQL = [NSString stringWithFormat:#"Update Expense_Group set Expense_sum = Expense_sum-\%#\ where Expense_Type_Id = \"%#\"",strExpAmount,current_Expense_TypeId];
and now,i got this warning "Unknown escape sequence '\x20'".
You only need to "\" escape the quotes within that "stringWithFormat:" call...
NSString *insertSQL = [NSString stringWithFormat:#"Update Expense_Group set Expense_sum = Expense_sum-%# where Expense_Type_Id = %#",strExpAmount,current_Expense_TypeId];
This may solve the issue no need of escape sequence before %#.
NSString *insertSQL = [NSString stringWithFormat:#"Update Expense_Group set Expense_sum = Expense_sum-'%#' where Expense_Type_Id = '%#'",strExpAmount,current_Expense_TypeId];

Inserting a string into another string

I am working on iPhone app. I have the SQL query:
NSString *myRed = [NSString stringWithFormat: #"%1.4f", slideR.value];
[self insertData:#"INSERT INTO colour VALUES("+myRed+")"];
It produces syntax error. How to insert a string into string. That approach in Java would have worked.
Best regards
Try this :
NSString *myRed = [NSString stringWithFormat: #"%1.4f", slideR.value];
[self insertData:[NSString stringWithFormat:#"INSERT INTO colour VALUES(\"%#\")",myRed]];
If you don't want " then :-
[self insertData:[NSString stringWithFormat:#"INSERT INTO colour VALUES(%#)",myRed]];
Hope it helps you.
You could try writing your query and your string appended together before hand.
NSString *str = #"Your values";
NSString *query = [NSString stringWithFormat:#"INSERT INTO color VALUES(%#)", str];
[self insertData:query];

Resources