i converting Hebrew string and trying yo add it to array , this is the code:
NSMutableArray * myarray;
// #property(strong,nonatomic) NSMutableArray *stringArray;
for (NSString * item in stringArray) {
NSData * UTF8 = [item dataUsingEncoding:NSUTF8StringEncoding];
NSString * myString = [[NSString alloc] initWithData:UTF8 encoding:NSUTF8StringEncoding];
NSLog(#"myString = %#",myString); // Hebrew str log fine
[myarray addObject:myString];
}
NSString * ConvetData = [NSString stringWithFormat:#"%#",myarray];
NSLog(#"ConvetData = %#",ConvetData); // null
newitem.ingredient = ConvetData;
the array coms out as null , how can i crate one string from my convert data?
You need to initialize myarray: NSMutableArray *myarray =[[NSMutableArray alloc] init];
Related
Using Json I have retrieve some values to UITableViewCell
NSString *sample = [[array objectAtIndex:indexPath.row] objectForKey:#"food"];
I am getting 1,2,3,4,6 in my sample string values
But I want to show like, if for example 1=apple 2=grape 3=orange 4=pineapple 5=lemon 6=All
I want the result to be apple,grape,orange,pineapple,All in my sample string values
In Objective-C there is no built in enums available for String types. You can declare enums as Integer and then make a function or an array of strings which return the string value for that enum.
Declare in your .h file.
typedef NS_ENUM(NSInteger, FRUIT) {
APPLE = 1,
GRAPE,
ORANGE,
PINEAPPLE,
LEMON,
All
};
extern NSString * const FRUITString[];
Define in your .m file.
NSString * const FRUITString[] = {
[APPLE] = #"APPLE",
[GRAPE] = #"GRAPE",
[ORANGE] = #"ORANGE",
[PINEAPPLE] = #"PINEAPPLE",
[LEMON] = #"LEMON",
[All] = #"All"
};
and you can use it like:
NSLog(#"%#", FRUITString[APPLE]);
// OR
NSLog(#"%#", FRUITString[1]);
Output: APPLE
Based on your comment: if food values is "food":"1,2" I want to show "apple,grape"
NSString *foodValue = #"1,2";
NSArray *values = [foodValue componentsSeparatedByString:#","];
NSMutableArray *stringValues = [[NSMutableArray alloc] init];
for (NSString *value in values) {
[stringValues addObject:FRUITString[[value integerValue]]];
}
NSLog(#"%#", [stringValues componentsJoinedByString:#","]);
APPLE,GRAPE
Change your code objectForKey#"" to valueForKey#""
NSString *sample = [[array objectAtIndex:indexPath.row] valueForKey:#"food"];
Define a NSDictionary with your required mapping, and fetch the value from your mapping dictionary:
NSDictionary *foodDict = #{
#"1": #"apple",
#"2": #"grape",
#"3": #"orange",
#"4": #"pineapple",
#"5": #"lemon",
#"6": #"all"
};
NSString *sample = [[array objectAtIndex:indexPath.row] objectForKey:#"food"];
NSString *sampleValue= [foodDict valueForKey:sample];
"sampleValue" is your required String Value needed. (Cast it to NSString if required)
this is my json content.
[
{
"sha":"30eae8a47d0203ac81699d8fc2ab2632de2d0bba",
"commit":{
"author":{
"name":"Madhura Bhave",
"email":"mbhave#pivotal.io",
"date":"2017-03-23T23:14:32Z"
},
"committer":{
"name":"Madhura Bhave",
"email":"mbhave#pivotal.io",
"date":"2017-03-23T23:14:32Z"
},
"message":"Merge branch '1.5.x'",
}
}
]
and this is my main.i just want to retrieve key value from message and name,email,date from committer dictionary.i got stuck how to do that.
NSMutableArray *CommitArray = [[NSMutableArray alloc] init];
for (NSDictionary *CommitDictionary in CommitJson) {
CommitDict *commitDictObj = [[CommitDict alloc] init];
commitDictObj.message = [CommitDictionary objectForKey:#"message"];
for (NSDictionary *CommitterDictionary in [CommitDictionary objectForKey:#"committer"]) {
Committer *author = [[Committer alloc] init];
author.name = [CommitterDictionary objectForKey:#"name"];
author.email = [CommitterDictionary objectForKey:#"email"];
author.date = [CommitterDictionary objectForKey:#"date"];
}
[CommitArray addObject:commitDictObj];
}
for (int i =0 ; i < [CommitArray count] ; i++){
CommitDict *commitDictObj = [CommitArray objectAtIndex:i];
NSLog(#"Commit Message: %#", commitDictObj.message);
}
return 0;
}
}
i try fetch the json and display it value of message,name,email and date.how can i log the value of message, name, email and date?
Your array contains a dictionary, and that dictionary contains the commit dictionary, not the commit dictionary directly. Replace that part of your code:
for (NSDictionary *CommitDictionary in CommitJson) {
CommitDict *commitDictObj = [[CommitDict alloc] init];
With that:
for (NSDictionary *shaCommitDictionary in CommitJson) {
CommitDict *commitDictObj = [[CommitDict alloc] init];
NSDictionary *CommitDictionary = [shaCommitDictionary objectForKey:#"commit"];
(1) Convert JSON to NSDictionary
NSData *jsonData= ... // Assume you got the data already loaded
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
(2) Access the dictionary values (fast enumeration available by now!!
NSString *message = dictionary[#"message"];
NSDictionary *author = dictionary[#"author"];
NSString *name = author[#"author"];
NSString *email = author[#"author"];
NSString *date = author[#"author"];
// OR:
// NSString *name = dictionary[#"author"][#"author"];
// NSString *email = dictionary[#"author"][#"author"];
// NSString *date = dictionary[#"author"][#"author"];
And thats it. I think the tricky thing is to get the JSON Data to the NSDictionary?
See here: https://stackoverflow.com/a/30561781/464016
i've an NSString like this:
NSString *word = #"119,111,114,100"
So, what i want to do is to convert this NSString to word
So the question is, in which way can i convert a string to a word?
// I have added some values to your sample input :-)
NSString *word = #"119,111,114,100,32,240,159,145,141";
// Separate components into array:
NSArray *array = [word componentsSeparatedByString:#","];
// Create NSData containing the bytes:
NSMutableData *data = [[NSMutableData alloc] initWithLength:[array count]];
uint8_t *bytes = [data mutableBytes];
for (NSUInteger i = 0; i < [array count]; i++) {
bytes[i] = [array[i] intValue];
}
// Convert to NSString (interpreting the bytes as UTF-8):
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"%#", str);
Output:
word 👍
Try this:
NSString *word = #"119,111,114,100";
NSArray *array=[word componentsSeparatedByString:#","];
for (NSString *string in array) {
char character=[string integerValue];
NSLog(#"%c",character);
}
Output:
w
o
r
d
libicu it's an UTF8 library that supports a conversion from an array of bytes as stated here.
The thing is, it offers Java, C or C++ APIs, not obj-c.
I one of my apps, i parse some data from local host and print it in a table view. To get the data, the user first logs in using an alert view. The user id entered is then used to fetch the data which i parse using JSON.
There is definitely a very simple solution to this question but I can't seem to be able to fix it. The problem is that when I print the data the string comes in this format:
( "string" )
But I want it so that it just says : string
in the table view. Here is my parsing method:
- (void)updateMyBooks
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Fetch data on a background thread:
NSString *authFormatString =
#"http://localhost:8888/Jineel_lib/bookBorrowed.php?uid=%#";
NSString *string = [[NSString alloc]initWithFormat:#"%#",UserID];
NSString *urlString = [NSString stringWithFormat:authFormatString, string];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(#"uel is %#", url);
NSString *contents = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
response1 = [contents JSONValue];
if (contents) {
// ... Parse JSON response and add objects to newBooksBorrowed ...
BookName = [[NSString alloc]init];
DateBorrowed = [[NSString alloc]init];
BookID = [[NSString alloc]init];
BookExtended = [[NSString alloc]init];
BookReturned = [[NSString alloc]init];
BookName = [response1 valueForKey:#"BookName"];
BookID = [response1 valueForKey:#"BookID"];
DateBorrowed = [response1 valueForKey:#"DateBorrowed"];
BookExtended = [response1 valueForKey:#"Extended"];
BookReturned = [response1 valueForKey:#"Returned"];
dispatch_sync(dispatch_get_main_queue(), ^{
// Update data source array and reload table view.
[BooksBorrowed addObject:BookName];
NSLog(#"bookBorrowed array = %#",BooksBorrowed);
[self.tableView reloadData];
});
}
});
}
This is how I print it in the table view:
NSString *string = [[NSString alloc] initWithFormat:#"%#",[BooksBorrowed objectAtIndex:indexPath.row]];
NSLog(#"string is %#",string);
cell.textLabel.text = string;
When I use log during the parsing process, it comes out as ( "string" ) so the problem is somewhere in the parsing, at least thats what I think.
If
NSString *string = [[NSString alloc] initWithFormat:#"%#",[BooksBorrowed objectAtIndex:indexPath.row]];
returns something like "( string )" then the most probably reason is that
[BooksBorrowed objectAtIndex:indexPath.row]
is not a string, but an array containing a string. In that case,
NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];
should be the solution.
NSString *string = [[NSString alloc] initWithFormat:#"%#",[BooksBorrowed objectAtIndex:indexPath.row]];
string = [string stringByReplacingOccurrencesOfString:#"(" withString:#""];
string = [string stringByReplacingOccurrencesOfString:#")" withString:#""];
string = [string stringByReplacingOccurrencesOfString:#"\"" withString:#""];
NSLog(#"string is %#",string);
cell.textLabel.text = string;
EDIT:
Above code is used if it's showing text in that format in your label.
If you see that in NSlog then it's NSString inside NSArray.
You need to fetch that string first from array and then display, use code line suggested by #Martin R for that.
NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];
I have a array that's I want to have in a uitextview. This is how it's currently displaying. How do I remove the ( ( ) )'s?
Here's a screenshot
Here's my code:
NSArray *arrayConditions = [[NSArray alloc] initWithObjects:[item objectForKey:#"conditions"], nil];
NSString * resultConditions = [arrayConditions description];
self.conditionsDeal.text = resultConditions;
NSLog(#"conditions are %#", [item objectForKey:#"conditions"]);
thanks for any help
NSArray *arrayConditions = [[NSArray alloc] initWithObjects:[item objectForKey:#"conditions"], nil];
// change this line only for the concatenation
NSString * resultConditions = [arrayConditions componentsJoinedByString:#"\n"];
self.conditionsDeal.text = resultConditions;
// this line always provides you the result of the '-description' method of the 'NSArray'
NSLog(#"conditions are %#", [item objectForKey:#"conditions"]);
NSMutableString *textViewText = [NSMutableString string];
for (NSString *str in arrayConditions)
[textViewText appendFormat:#"%#\n", str];
self.conditionsDeal.text = textViewText;
Convert that array into NSMutableString.
Separate that string by "," or space and write it UITextView.