How to get NSString value from NSPopupButton dropdown list? - objective-c

Get username values from sqlite db.
-(NSArray*)getUname
{
NSArray *resul = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSArray *fetchedRecords = [resul valueForKey:#"uName"];
}
Insert this array values into NSPopupButton
[_registeredUserPopupButton addItemsWithTitles:[self getUname]];
Get a string value from NSPopupButton dropdown list
NSString *usrNam = [NSString stringWithFormat:#"%#",[_registeredUserPopupButton selectedItem]];
From the above code usrNam value returns like below
"NSMenuItem: 0x6080000a9c00 mickel"
but i want my nsstring output as "mickel"

Just get the title from the menu item:
NSString *usrNam = [[_registeredUserPopupButton selectedItem] title];
or with dot notation
NSString *usrNam = _registeredUserPopupButton.selectedItem.title;

The -selectedItem method returns a NSMenuItem object. This in turn has a title property which is what I think you are looking for. Should be as below I think.
NSString *usrNam = [NSString stringWithFormat:#"%#",[_registeredUserPopupButton selectedItem.title]];

Related

Objective C - UITextField Live Search an NSMutableArray and Output to UITableView

Similar to the WhatsApp 'Add Participants' to a new group feature, in my app I have a UITextField where the user can begin searching for contacts which also have the app installed to add to a group chat.
I store all of the available contacts in an NSMutableArray searchableContacts with the following keys
"firstName"
"lastName"
"phoneNumber"
Using the following delegate method, I would like to check the current input in the UITextField and see if it matches either part of the firstName OR lastName in the searchableContacts array:
-(void)textFieldDidChange :(UITextField *)theTextField{
// Check if the user input matches the beginning of either firstName or lastName in contactsArray. If so, output that user information to the tableView
}
You can use an NSPredicate to filter the array.
For example like this:
NSArray *users = #[
[[User alloc] initWithFirstName:#"Test" lastName:#"Me"],
[[User alloc] initWithFirstName:#"Fooo" lastName:#"bar"]
];
NSString *searchTerm = #"bar";
NSArray *filteredArray = [users filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(User *user, NSDictionary *bindings) {
return [user.firstName containsString:searchTerm] || [user.lastName containsString:searchTerm];
}]];
NSLog(#"Filtered values: %#", filteredArray);
searchTerm in your case would be the theTextField.text property value. Filtered array contains all matched contacts.
Cheers

Save information from NSTextField in sqlite database (objective-c)

Since some months I am interested in coding. At the moment I am working on a programm which saves informations from textfields in a sqlite3 database. I am programming this project on Objective C in Xcode for Mac. Unfortunately I have problems in inserting data into my database. When I insert it and open my database in Terminal I just get this information in the cell for the string:
Here ist my code:
NSString *Bt = [NSString stringWithFormat:#"%#", Text]; //Theres a IBOutlet NSTextField *Text; in the Header data of the class
dbPath=#"/Users/Desktop/database.db";
if (sqlite3_open([dbPath UTF8String], &database)!=SQLITE_OK) {
sqlite3_close(database);
NSLog(#"Failed to open database");
} else {
char *errorMessage;
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO table (table) VALUES (\"%#\")", Bt];
const char *insert_stmt = [insertSQL UTF8String];
if(sqlite3_exec(database, insert_stmt, NULL, NULL, &errorMessage)==SQLITE_OK){
NSLog(#"Data inserted");
[db_status setFloatValue:5];
}
}
}
It would be great if somebody can help me. I think it is just a small error but I don´t get the problem ;(
Best regards,
Robby
by the looks of your IBOUTLET connection, how you have your code, Bt is equal to the TextField. But what you want is the text value in the TextField. so you would use this: NSString *Bt = [NSString stringWithFormat:#"%#", Text.stringValue]; NSTextField has a property named stringValue and this will return the NSString that contains the value inside the textfield, Text.
Allowing Quotation Marks in Your Database
So easiest way, i can think of, is having a category method on NSString. So wherever you're going to save string or text from the user to your database, you would use this function to then insert into your format statement. and example shown below:
Header File of NSString Category
#interface NSString (STRING_)
/**
* Allowing Qoutation marks inside a string to be saved in a SQL column with the string format sourrounded by #"\"%#\"". this will not modify the string, only return a modified copy
* #return NSString *
*/
- (NSString *)reformatForSQLQuries;
#end
Implantation File of NSString Category
#implementation NSString (STRING_)
- (NSString *)reformatForSQLQuries {
NSString *string = self;
//Depending on how you "wrap" your strings to format the string values into your INSERT, UPDATE, or DELETE queries, you'll only have to use one of these statements. YOU DO NOT NEED BOTH
//Use this if you have your format as: '%#'
string = [string stringByReplacingOccurrencesOfString: #"'" withString: #"''"];
//Use this if you have your format as: \"%#\"
string = [string stringByReplacingOccurrencesOfString: #"\"" withString: #"\"\""];
return string;
}
example
...
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO table (table) VALUES (\"%#\")", [Bt reformatForSQLQuries]];
...
I got the solution ! It´s false to write:
NSString *Bt = [NSString stringWithFormat:#"%#", Text];
The solution is to get the value from the NSTextField:
NSString *Bt = [NSTextField stringValue];
What I still don´t understand is why it´s not possible to save Strings which include this symbol: "". For example:
The database saves this: I like programming.
But not this: I like "programming".
Can somebody explain me why ?

NSString Somehow turn in to an "__NSArrayI" object

i am receiving response from my server, it looks like this :
2012-09-12 16:29:11.690 WhatIsIt[1763:707] (
{
qid = ebb81a9c0c2125c9f12fee33c281dfe2ef5c1596;
"qid_data" = {
labels = Wristwatch;
};
} )
when i am parsing the "qid" value like this :
- (void)updateCompleteWithResults:(NSArray*)results{
NSLog(#"%#",results);
NSString *qid = [results valueForKey:#"qid"];
the NSString object is getting a Parentheses around the string (i dont know how)
looks like this :
2012-09-12 16:34:17.979 WhatIsIt[1785:707] (
2e1da5854f3b4f02cd967293cd1364e6d3e0b76a
)
so i tried to use :
NSString *string = #" spaces in front and at the end ";
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
and the app crashes, any idea?
The problem is the root object of this structure is an array...
If you look at the valueForKey: method on NSArray you'll see that it will actually call valueForKey: on each of it's members and return the results in another array (this is what you are seeing).
What you should do instead is first get the object you are interested in and then work with it on it's own
NSDictionary *myObject = [whatIsIt objectAtIndex:0];
NSString *qId = [myObject objectForKey:#"qid"];

Variable not working

I want to set a variable value. But I don't know how to. I want replace 3456 with "value".
Code:
NSString *value = textbox.text;
NSString *field = [captcha stringByEvaluatingJavaScriptFromString:#"document.getElementById('mainPagePart:rn3').value='3456';"];
I think what you're trying to say is you want to put the contents of value into the string?
NSString *field = [captcha stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"document.getElementById('mainPagePart:rn3').value='%#';", value]];

NSMutableArray with NSDictionary to Labels without the surrounding brackets

I'm making a scoreboard for my game. And when I do a NSLog of the data it comes out as this:
{
name = TTY;
score = "3.366347";
}
So my question is how do I remove this brackets and also just grab the name (TTY) and score (3.36 without the quotations) and place them in variable for me to put into labels.
Currently I can place them in labels but they have the lingering curly braces "{" and "}".
Any hints would be helpful as I'm happy to search further I just don't know the vocab to search for it.
thanks
For NSDictionary if you want to get the values you use objectForKey: method;
[scoreDictionary objectForKey:#"name"];
In your case the method will return TTY
You can store this in a variable like normal:
NSString *entryName = [scoreDictionary objectForKey:#"name"];
For NSArray (and NSMutableArray) you use objectAtIndex: method;
[scoresArray objectAtIndex:0];
I'm guessing that you have many NSDictionary in the NSArray, in which case you can combine the two methods above and get;
[[scoresArray objectAtIndex:0] objectForKey:#"name"];
which will give you the value of name in the first dictionary of the array.
Also if you want to access multiple key values you can use;
NSDictionary *entry = [scoresArray objectAtIndex:0];
NSString *entryName = [entry objectForKey:#"name"];
NSString *entryScore = [entry objectForKey:#"score"];