I have a plist with Array title and item0,item1,item2 is dictionary to store title and password of my app.now i want to check for signin process
now i Want to check for pass and items should be matched when i press submit button to login.i Tried the below code but xcode crashes
-(void)authenticateCredentials
{
NSMutableArray *plistArray = [NSMutableArray arrayWithArray:[self readFromPlist]];
for (int i = 0; i< [plistArray count]; i++)
{
if ([[[plistArray objectAtIndex:i]objectForKey:#"pass"]isEqualToString:emailTextFeild.text] && [[[plistArray objectAtIndex:i]objectForKey:#"title"]isEqualToString:passwordTextFeild.text])
{
NSLog(#"Correct credentials");
return;
}
NSLog(#"INCorrect credentials");
}
}
and
-(NSArray*)readFromPlist
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:#"XYZ.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];
NSArray *valueArray = [dict objectForKey:#"title"];
return valueArray;
}
the xcode gets crashs ..please check in -(void)authenticateCredentials where i am incorrect?
AND
Crash when I select the submit button,Its wont give any output correct or incorrect in nslog and Xcode crash then , with errorr
2012-12-14 12:10:45.142 NavTutorial[1661:f803] emailEntry: hi#gmail.com.com
2012-12-14 12:10:45.145 NavTutorial[1661:f803] passwordEntry: hellohello
2012-12-14 12:10:45.153 NavTutorial[1661:f803] -[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x1568cd8
2012-12-14 12:10:45.155 NavTutorial[1661:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x1568cd8'
*** First throw call stack:
(0x14d7052 0x11c2d0a 0x14d8ced 0x143df00 0x143dce2 0x3981 0x14d8ec9 0x3735c2 0x37355a 0x418b76 0x41903f 0x417e22 0x39893f 0x398c56 0x37f384 0x372aa9 0x1bcdfa9 0x14ab1c5 0x1410022 0x140e90a 0x140ddb4 0x140dccb 0x1bcc879 0x1bcc93e 0x370a9b 0x211d 0x2095 0x1)
terminate called throwing an exception
and
two days ago this same code worked fine
and if i put Breakpoint for IF condition and later if fails just after if condition.
Modify your authenticateCredentials as shown below and check if it is working. If it is showing an error message as Error! Not a dictionary, you need to check if your plist is having correct structure. Mostly your [objDict objectForKey:#"pass"] is returning a different data type.
- (void)authenticateCredentials {
NSMutableArray *plistArray = [NSMutableArray arrayWithArray:[self readFromPlist]];
for (int i = 0; i< [plistArray count]; i++)
{
id object = [plistArray objectAtIndex:i];
if ([object isKindOfClass:[NSDictionary class]]) {
NSDictionary *objDict = (NSDictionary *)object;
if ([[objDict objectForKey:#"pass"] isEqualToString:emailTextFeild.text] && [[objDict objectForKey:#"title"] isEqualToString:passwordTextFeild.text])
{
NSLog(#"Correct credentials");
return;
}
NSLog(#"INCorrect credentials");
} else {
NSLog(#"Error! Not a dictionary");
}
}
}
Related
I am using Facebook api to get user's friends name, UID and birthday. The problem I have is that how do I get to those values once I get the results back. Meaning, I want to get to username, birthdate for each returned value?
This code keeps crashing for me here
//set query
NSString *query = #"SELECT uid, name, birthday_date FROM user WHERE uid IN " #"(SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 25)";
// Set up the query parameter
NSDictionary *queryParam = [NSDictionary dictionaryWithObjectsAndKeys:query, #"q", nil];
[FBRequestConnection startWithGraphPath:#"/fql"
parameters:queryParam
HTTPMethod:#"GET"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (error) {
NSLog(#"Error: %#", [error localizedDescription]);
}
else {
NSLog(#"Result: %#", result);
NSArray *friendInfo = (NSArray *) [result objectForKey:#"data"];
NSArray *arryData3 = [[NSArray alloc] initWithArray:friendInfo];
NSLog(#"[friendInfo count]: %d ... [arryData3 count]: %d ...",[friendInfo count],[arryData3 count]);
for (int i=0; i<[arryData3 count]; i++) {
NSArray *bdayArray = [[NSArray alloc] initWithArray:[arryData3 objectAtIndex:i]];
//CODE CRASH HERE!!!!
NSString *bdayStr = [bdayArray objectAtIndex:0];
NSString *nameStr = [bdayArray objectAtIndex:1];
}
}
}];
This is what my crash log looks like
data(
{
"birthday_date" = "01/06";
name = "John Doe";
uid = 555555;
},
{
"birthday_date" = "09/01/1984";
name = "Mrs. Smith";
uid = 4444444;
},
)
[friendInfo count]: 2 ... [arryData3 count]: 2 ...
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSArray initWithArray:range:copyItems:]: array argument is not an NSArray'
*** First throw call stack:
(0x168f052 0x1cabd0a 0x167fe0b 0x15e8170 0x16ca2 0x27c03 0x26c7e 0x2444e 0x2927e 0x296e7 0xfc1a59 0xfbfe94 0xfc0eb7 0xfbfe4f 0xfbffd5 0xf04f6a 0x3964bbd 0x3a315ea 0x395b298 0x3a3116b 0x395b137 0x166397f 0x15c6b73 0x15c6454 0x15c5db4 0x15c5ccb 0x3bd1879 0x3bd193e 0x5cca9b 0x2038 0x1f95)
For anyone that maybe interested in future. This is how you get to individual values in the results of Facebook.
NSLog(#"Result: %#", result);
NSArray *friendInfo = (NSArray *) [result objectForKey:#"data"];
NSLog(#"[friendInfo count]: %d ....",[friendInfo count]);
for (int i=0; i<[friendInfo count]; i++)
{
NSMutableString *userNameStr = [NSMutableString stringWithFormat:#"%#", [[friendInfo objectAtIndex:i] objectForKey:#"name"]];
NSMutableString *userDateStr = [NSMutableString stringWithFormat:#"%#", [[friendInfo objectAtIndex:i] objectForKey:#"birthday_date"]];
NSLog(#"userNameStr: %# ... userDateStr: %# ...", userNameStr,userDateStr);
//From this point onwards you can put these individual strings into an array
//and if you had defined the array globally you can get to these results from
//any function in your code
}
The return is a dictionary, not an array.
Please read about NSDictionary and use it just like you used the result - objectForKey function. In this case the key is birthday_date and then name for the strings in the for function.
Liviu
I'm new to objective-c and I decided to start by working through Stanford's CS193s 2010F Session's lectures/assignments.
I was working on the second assignment, and I was stuck when I had to return a NSString that combines(concatenates) every strings inside NSMutableArray. (The MutableArray consists only NSString at its each indices)
My approach was to use a for loop to pass through the MutableArray's indicies(in the code below, the MutableArray is 'anExpression' with type 'id'). I declared a NSMutableString and added NSString at each indices of 'anExpression' array. Here is the code:
+ (NSString *)descriptionOfExpression:(id)anExpression{
NSMutableString *result = [NSMutableString string];
for (int i = 0;i<[anExpression count];i++){
[result appendString:[anExpression objectAtIndex:i]];
}
return result;
}
However, at
[result appendString:[anExpression objectAtIndex:i]];
xcode crashes with following error statements:
2012-07-17 01:44:51.014 Calculator[9470:f803] -[__NSCFNumber length]: unrecognized selector sent to instance 0x68732c0
2012-07-17 01:44:51.015 Calculator[9470:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x68732c0'
*** First throw call stack:
(0x13ca022 0x155bcd6 0x13cbcbd 0x1330ed0 0x1330cb2 0x12d2d18 0x13460d7 0x1397a8d 0x3a50 0x27ed 0x13cbe99 0x1714e 0x170e6 0xbdade 0xbdfa7 0xbd266 0x3c3c0 0x3c5e6 0x22dc4 0x16634 0x12b4ef5 0x139e195 0x1302ff2 0x13018da 0x1300d84 0x1300c9b 0x12b37d8 0x12b388a 0x14626 0x1ed2 0x1e45 0x1)
terminate called throwing an exception
I looked through apple's developer's document, saw 'NSString stringWithFormat:' method, and decided to use this method instead:
+ (NSString *)descriptionOfExpression:(id)anExpression{
NSMutableString *result = [NSMutableString string];
for (int i = 0;i<[anExpression count];i++){
[result appendString:[NSString stringWithFormat:#"%#",[anExpression objectAtIndex:i]]];
}
return result;
}
and it works now.
now I'm confused why second code works but the first doesn't.
I thought appending string only fails(and crashes) when it's passed a nil...
Is there something I'm missing?
Thank you in advance :)
It looks like the contents of anExpression are instances of NSNumber instead of NSString. The error you are getting is a hint as to how appendString works; it's first step is obviously to ask the passed "string" how long it is (presumably so it can allocate enough memory). This is obviously not a method on NSNumber (hence the crash) and stringWithFormat is designed to do type-checking and be more flexible.
I suspect you could also use stringValue just as well:
[result appendString:[[anExpression objectAtIndex:i] stringValue]];
+ (NSString *)descriptionOfExpression:(id)anExpression{
NSMutableString *result = [NSMutableString string];
for (int i = 0;i<[anExpression count];i++) {
[result appendString:[[anExpression objectAtIndex:i] stringValue]];
}
return result;
}
Converting it into string would help you!!
Use this function:
+ (NSString *)descriptionOfExpression:(id)anExpression
{
NSMutableString *result = [[NSMutableString alloc] init];
for (int i = 0;i<[anExpression count];i++)
{
NSString *str = [NSString stringWithFormat:#"%#",[anExpression objectAtIndex:i]];
if(str)
{
[result appendString:str];
}
//OR
//[result appendFormat:[NSString stringWithFormat:#"%#",[anExpression objectAtIndex:i]]];
}
return result;
}
i was getting familiar with sqlite and try to insert the values into database but giving exception. tf1 and tf2 are object of textfield. here is my code:
#synthesize tf1, tf2, add, disp;
-(id)initApp
{
if(![super init])
return nil;
//DB stored in application bundle
NSString *DBName = #"kbase.sqlite";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingString:DBName];
db = [FMDatabase databaseWithPath:path];
[db setLogsErrors:TRUE];
[db setTraceExecution:TRUE];
if(![db open])
{
NSLog(#"Could not open Database");
return 0;
}
else
{
NSLog(#"Database opened successfully");
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initApp];
[add addTarget:self action:#selector(insertButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[disp addTarget:self action:#selector(displaytButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
-(IBAction)insertButtonClicked :(id)sender
{
NSLog(#"insert");
NSLog(#"db.....%#",db);
[db executeUpdate:#"insert into info values (?,?)", tf1.text, tf2.text];
[self resignFirstResponder];
}
-(IBAction)displayButtonClicked:(id)sender
{
NSString *str1 = tf2.text;
NSString *returnResult = [[NSString alloc]init];
FMResultSet *rs = [db executeQuery:#"select name from info where rollno = 1"];
while ([rs next])
{
returnResult = [rs stringForColumn:#"rollno"];
[tf1 setText:returnResult];
[tf2 setText:returnResult];
}
}
databse is opening successfully and controll terminate after printing insert
exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL executeQuery:]: unrecognized selector sent to instance 0x3d30ef0'
i am not able to configure the problem. plz help.
I think make your method
[db executeUpdate:#"insert into info values (?,?)" withValue1:tf1.text withValue2:tf2.text];
Instead of
[db executeUpdate:#"insert into info values (?,?)", tf1.text, tf2.text];
it seems that db does not exist in this context. If db is a class variable, make sure it is not released somewhere else. Simply put an NSLog(#"%#", db); above the execution to test it.
I have the following code:
NSDictionary *dict = [[NSDictionary alloc]
initWithObjectsAndKeys:myarray1, #"array1", myarray2, #"array2" nil];
NSArray *shorts =[[dict allKeys] sortedArrayUsingSelector:#selector(compare:)];
for (NSString *dir in shorts) {
NSArray *tempArr = [dict objectForKey:dir];
for (NSString *file in tempArr ) {
NSLog(#"%#", file);
}
}
Where myarray1 and myarray2 are NSArrays.
When I execute the code the application crashes with:
-[NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x1d134
This is apparently the tempArr, which is not recognized as an NSArray. I know that [dicFiles objectForKey:dir] returns an id type object, but as a generic type, I cannot get what I'm doing wrong.
You haven't included the code that initializes myarray1 and myarray2, but apparently one or both of them are instances of NSString rather than NSArray. You can check that after retrieving one of the objects from the array as follows:
if (![tempArr isKindOfClass:[NSArray class]])
{
NSLog(#"Unable to process temp array because it's an instance of %#", [tempArr class]);
}
else
{
// for loop code goes here...
}
I've been playing around with a search facility for my application table view for a while now trying to get it working but i keep getting the same error in my console.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' [NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance
I believe that this following section may be the problem I have tried passing some NSLog entries inside the if statement and it seems to get through it but the problem is when I click on the search bar and starting typing, the first letter I type calls the error and cancels my app.
Here is where the problem is
In View Will Appear "Food" Array is initialized as below:
NSString *myDBnew =#"/Users/taxsmart/Documents/rw3app.sql";
database = [[Sqlite alloc] init];
[database open:myDBnew];
NSString *quer = [NSString stringWithFormat:#"Select category from foodcat"];
Food = [database executeQuery:quer];
//[database executeNonQuery:quer];
[database close];
Search bar delegate method where error is encountered:
(void) searchTableView
{
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
// [searchArray addObjectsFromArray:Food];
for(NSDictionary *dictionary in Food)
{
NSString temp1 = [dictionary objectForKey:#"category"];
[searchArray addObject:temp1];
}
for (NSString *sTemp in searchArray)
{
NSLog(#"Value: %#",NSStringFromClass([sTemp class]));
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
[searchArray release];
searchArray = nil;
}
What should I do?
Please Help.
Please Suggest
Thanks
It looks that result of database query (Food) is dictionary that contains dictionary. This code:
for(NSDictionary *dictionary in Food)
{
NSString temp1 = [dictionary objectForKey:#"category"];
[searchArray addObject:temp1];
}
can be replaced with:
for(NSDictionary *dictionary in Food)
{
NSObject *ob = [dictionary objectForKey:#"category"];
if([ob isKindOfClass: [NSString class]])
{
[searchArray addObject:ob];
}
else if([ob isKindOfClass: [NSDictionary class]])
{
NSDictonary *dic1 = (NSDictionary*)ob;
// ... at this point you can get the string for desired dictionary key
// or you can ignore it
}
}
With this code we can be sure that only strings are put into searchArray.
If you want to make full tree parsing for desired key 'category' then you should make some recursive function to search the dictionary.
You can dump Food variable to console to see at which leaf is actually the result you are looking for. Put the break-point and into console type 'po Food'.
Appears that there is an NSDictionary in your dataArray.
Add an
NSLog(#"%#",NSStringFromClass([description class]]));
To see which classes your dataArray contains.