NSString key for value [closed] - ios

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
NSString *userUpdate = [NSString stringWithFormat:#"grant_type=password&Password=%#&UserName=%#&SchoolId=1",[params valueForKey:#"UserName"#"Password"]];
When I am running this code I am getting an error. Is this code correct?

The problem in your code is you have written %# twice and supplied only single value, that is the main reason behind error.
In NSDictionary you can get only one value at a the time using valueForKey.
Use this code
NSString *userUpdate = [NSString stringWithFormat:#"grant_type=password&Password=%#&UserName=%#&SchoolId=1",[params valueForKey:#"Password"],[params valueForKey:#"UserName"]];

In NSDictionary Get only one value using valueForKey.
For Getting Password access like [params valueForKey:#"Password"]
For UserName use syntax like [params valueForKey:#"UserName"]
Can't get both value same time use like [params valueForKey:#"UserName"#"Password"]
Below is sample code:
NSString *userUpdate = [NSString stringWithFormat:#"grant_type=password&Password=%#&UserName=%#&SchoolId=1",[params valueForKey:#"Password"],[params valueForKey:#"UserName"]];

Related

Delete row of mySQL table from iOS app [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm currently working on an iOS app that takes information from a mySQL database and outputs it to JSON which in turn is displayed in a UITableView. I would like to be able to delete a row of the UITableView, which would also delete the corresponding row in the database. I know I need to use this function:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[_wordsArray removeObjectAtIndex:indexPath.row];
[self.editTableView reloadData];
}
I'm not sure how it works, I know I'll have to use a PHP script in order for the row to be deleted from the database. What needs to be done on the iOS side? Can anyone point me in the right direction?
Thanks in advance.
What needs to be done on the iOS side?
This is a pretty broad question. You have to be more specific what you need help with.
Generally speaking this is what needs to be done which I am sure you already knew.
Delete the record from your array and reload the table. I see you have already done that.
I am assuming you are storing that array locally somewhere (NSUserDefaults, plist etc), otherwise the user will need to get latest data from your server on every app launch and make sure the delete row doesn't reappear
You need to make a Async call to your php on your server
You would need to pass in values like what info has the user decided to delete and then execute that delete in the database
If you need code samples then google it my man.
Doing the same, kinda, using a mySQL database to save my data and using arrays to access the tables on the iOS level. What I do for inserting, updating and deleting records from the database is something like this:
Using mySQL on a iOS Level ##
mySQL to iOS level: first is to create a method, or inline function, somewhere that is accessible anywhere in the app; inside a singleton for an example. This method will return an array that contains one record by passing a sqlite3_statement in the parameter list. This method looks something like:
example
/**
* From the parameter list, an array is produced in Entry format
* #param [in] statement incoming value from previous call sqlite3_step(..) == SQLITE_ROW
* #return NSArray: Entry format
*/
static inline NSMutableArray * SQLStatementRowIntoEntryEntry( sqlite3_stmt *statement) {
...
return [NSMutableArray arrayWithObjects: stringSubject, date dateCreated, stringBody, emotion, weatherCondition, temperature isBookmarked, [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: sqlite3_column_int( statement, SQL_ENTRIES_id)], #"id", nil], nil];
};
How I save id keys from the mySQL record is put it in a dictionary as the last object, so it kinda stays out of the way while I'm using the array on the iOS level. But then is there ready whenever I want to update or delete this record from the database.
Updating and Deleting Records from iOS level: so because the id was saved in the array for each record this makes it remarkably easy to update or delete records in the database. In your query you'd have something like:
NSString *sqlStatement = [NSString stringWithFormat: #"UPDATE Entries SET subject = \"%#\", date = \"%#\", dateCreated = \"%#\", body = \"%#\", emotion = %d, weatherCondition = %d, temperature = %d, isBookmarked = %d, diaryID = %d where id = %d;", [[arrayEntry objectEntry_subject] stringByReformatingForSQLQuries], [dateFormatter stringFromDate: [arrayEntry objectEntry_date]], [dateFormatter stringFromDate: [arrayEntry objectEntry_dateCreated]], [[arrayEntry objectEntry_body] stringByReformatingForSQLQuries], [arrayEntry objectEntry_emotion], [arrayEntry objectEntry_weatherCondition], [arrayEntry objectEntry_temperature], [arrayEntry objectEntry_isBookmarked], [[[[[arrayEntry optionsDictionary] objectForKey: #"diary"] optionsDictionary] objectForKey: #"id"] intValue], [[[arrayEntry lastObject] objectForKey: #"id"] intValue]];
What you want is the [[[arrayEntry lastObject] objectForKey: #"id"] intValue] to take place of .. where id = %d; to either update or delete a record.
I hope this helps and do ask for any questions, I copied and pasted code from my project m
You can pass the Database query string to your php Script.
NSString *sqlquery = [NSSring sringwithFormate : #"DELETE FROM tabel_name WHERE tabel_id='your_tabel_row_id'"];
In this tabel_id is the your database row id which is you want to delete and your_tabel_row_id is the identifier which help to you to delete the record from the database.

JSON file parsing gives object name rather contents [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I do have a JSON file I created here: http://www.jsoneditoronline.org/?id=d0b62425c78f98db2398ed558f92e5cf
Simply, I added this file to my project and trying to parse it. For some reason, NSLog line gives me "statement" as a result rather than the statement object. I was expecting it to give me the whole dictionary of statement object instead. I am clearly missing something here. I would appreciate any help.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"math" ofType:#"json"];
NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:nil];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil];
NSLog(#"jsonObject is %#", jsonObject); //this gives me the whole JSON object correctly.
for (NSDictionary *dict in jsonObject[#"statements"]) {
NSLog(#"dict is %#", dict);
}
That is screwy JSON. Normally one would expect the value of "statements" to be an array of objects, not just a single object. When you iterate on a dictionary (JSON "object") you iterate through the keys, so you're getting what would be expected.

Combining arrays into a url string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
So I'm building an array of url strings using extensions. I have two arrays of extensions and Im a little bit confused on how to combine them.
I am trying to produce an array of string objects with the following format:
http://mywebsite.com/images/places/extention1/extention1
http://mywebsite.com/images/places/extention2/extention2
and so on...
When I try to combine them the way I know how I end up with the full array stuck in the place holders
http://mywebsite.com/images/places/full array1/full array 2
but what I'm really trying to do is build an array of those URL strings from the other two arrays
i know this is super simple but I haven't found any documentation on combining them into a skeleton string.
This would do the trick:
NSMutableArray *urlArray = [[NSMutableArray alloc] init];
NSArray *extensions = #[#"extension1", #"extension2", #"extension3"];
NSString *urlString = #"http://mywebsite.com/images/places/";
for (NSString *extension in extensions) {
NSString *combined = [[urlString stringByAppendingPathComponent:extension] stringByAppendingPathComponent:extension];
[urlArray addObject:combined];
}
There are tons of ways to do this but it could be as simple as:
NSArray *array = [NSArray arrayWithObjects:#"http://mywebsite.com/images/places/extention1/extention1", #"http://mywebsite.com/images/places/extention1/extention2", nil];

Get latest inserted object FatFractal [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm using FatFractal for my backend in a iPhone-app. Been searching the web and FatFractal docs without any luck.
I want to retrieve the latest object in a collection from a grabbag:
NSArray *result = [[Store ff] grabBagGetAllForObj:thread grabBagName:#"messages" withQuery:#"Get the latest inserted object" error:&err];
Probably the best way to do this is to construct the full query yourself and then use the standard getArrayFromUri: method. Taking your example, this would look like:
FatFractal *ff = [Store ff];
NSString *query = [NSString stringWithFormat:
#"%#/messages?sort=createdAt desc&start=0&count=1",
[ff metaDataForObj:thread].ffUrl];
NSArray *result = [ff getArrayFromUri:query];
For more info, see http://fatfractal.com/prod/docs/queries/#sorting-and-limiting-results.

ios when building a string I need to reuse the same placeholder [duplicate]

This question already has answers here:
Is there a way to specify argument position/index in NSString stringWithFormat?
(3 answers)
Closed 9 years ago.
I am making a string that is being used in a SQL statement. I need to reuse the same string within it several times. This string is the text from a textbox.
Current Code:
NSString *searchStr = [NSString stringWithFormat:#"(Name LIKE \'%%%#%%\' OR Contact LIKE \'%%%#%%\')", [self.txtfSearch text], [self.txtfSearch text]];
All the %'s are for a contains search. Please no comments on this method as I am connecting to a webservice that I have no control over currently. Im sure you can all relate.
What I need to do is reuse the [self.txtfSearch text] part at the end. I have looked up the apple dev docs on strings and placeholders. I even tried the C# method of using {0} for the placeholder but I cant get it working.
Doing it as above isnt a big deal if im looking in 2 columns, but there are many cases where its going to be 6 or more and then the string building gets ridiculous.
I want something like this:
NSString *searchStr = [NSString stringWithFormat:#"(Name LIKE \'%%{0}%%\' OR Contact LIKE \'%%{0}%%\')", [self.txtfSearch text]];
Is there a way to do this in obj-c?
Could you do something like this:
NSString *placeholderStr = #"_";
NSString *searchStr = [#"(Name LIKE '%%_%%' OR Contact LIKE '%%_%%'" stringByReplacingOccurrencesOfString: placeholderStr withString: [self.txtfSearch text]];
This will replace all of the underscores (_) with whatever text is in txtfSearch.

Resources