Hey all, I'm a total noob when it comes to Objective-C / iPhone development.
I'm trying to pull in text from a SQLite DB. I have a while loop that looks like this:
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
And within that loop, this prints to the log just fine:
NSLog(#"Text: %s",sqlite3_column_text(selectstmt, 1));
This does not work:
Category *categoryObj = [[Category alloc] initWithPrimaryKey:primaryKey];
categoryObj.categoryName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
NSLog(#"cat name: %s",categoryObj.categoryName);
When I run the above and look at the logs I see:
cat name: ‡}00å
I tried to write the field out to a label, thinking it might be something specific to the NSLog but nothing shows up there. Clearly I'm missing something fundamental but I'm at a loss for what it is.
Log your string with %# instead of %s and you'll be fine. NSStrings aren't pointers to characters, they're full-fledged objects, so you need to use the "object" placeholder in the log format string.
This has the added advantage of doing the right thing with non-ASCII strings and all of the other important things that NSString gives you.
Note that if you had just logged the result from SQLite directly instead of creating an NSString with it, then your %s would have been correct.
Remember: %s is for C strings, %# is for Objective-C objects.
Related
Okay, all I am doing is setting an NSString to a value with this code:
NSString *stringURL = [NSString stringWithFormat:#"http://api.themoviedb.org/3/movie/%#/trailers?api_key=1523229ded5824dab8bb7840782db266",searchID];
This is a string that I then turning into a URL for querying the TMDB database. This line of code gives me a BAD_EXC_ACCESS and it is blowing my mind because using this sort of NSString construction is something I have done thousands of times without a problem.
The one other thing to note is that this line is being executed right after another query call is made. The weird thing is that call makes the stringURL the same way, yet it works fine.
Any help would be appreciated...
You need to use %i to log an NSInteger, not %#
You need to use the following
NSString *stringURL = [NSString stringWithFormat:#"http://api.themoviedb.org/3/movie/%d/trailers?api_key=1523229ded5824dab8bb7840782db266",searchID];
Because searchID has NSInteger type and you are using "%#"
If it's an NSInteger you need to use %ld or you will got a warning, you can also use %d and explicitly cast to int via (int)searchID
Ok, I spent the last 8 hours fighting with it - it just seems beyond me. Here's my complete (relevant) code:
- (void)updateUserDefaults
{
NSMutableDictionary *viewControllerDetails = [[NSMutableDictionary alloc] initWithCapacity:4];
[viewControllerDetails setObject:[NSNumber numberWithInt:OOVenueClassControllerType] forKey:#"classType"];
[viewControllerDetails setObject:self.searchTerm forKey:#"searchTerm"];
[viewControllerDetails setObject:self.searchLocation forKey:#"searchLocation"];
//----- the next two lines cause the problem
NSString *res = [[NSString stringWithFormat:#"%#",[searchResults xmlString]] retain];
[viewControllerDetails setObject:res forKey:#"searchresults"];
//-----
NSMutableArray *viewControllersList = [NSMutableArray array] ;
[viewControllersList addObject:viewControllerDetails];
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
//the following line causes the error
[defs setObject:viewControllersList forKey:kViewControllersKey];
[defs synchronize];
[res release];
}
Note the block with the next two lines cause the problem. At first I didn't create another string, but added it later while trying to solve the problem.
If I comment out those two lines, everything works fine. If I put them back in, I get
- [CFString class]: message sent to deallocated instance 0xa1a9000
Is something is wrong with the string that I'm trying to put into the userdefaults? That string is rather large (about 200,000 characters), but I had stored even longer strings in user defaults in the past.
It's also worth noting that if I uninstall the app, then everything works fine. But on subsequent runs the problem exhibits itself.
So, how and why and where is the string getting deallocated? I have explicitly added retain - but that still doesn't help. What am I missing?
Edit: just realised I forgot to say that the error is thrown on line
[defs setObject:viewControllersList forKey:kViewControllersKey];
Also, for general information, method - (NSString *)xmlString on searchResults does exactly what the name means: creates an XML string with the information from that object.
Edit 2: I tried doing something else with that string - convert it to NSData, compress using zlib - but regardless of data type, that particular object gets deallocated. Does it have to do something with the size of the string?
NSString *res = [[NSString stringWithFormat:#"%#",[searchResults xmlString]] retain];
Is auto released. You don't need to release it at the end of your method. You are over-releasing it.
Further, you don't need to retain the [searchResults xmlString]. The stringWithFormat method already does it for you.
Good Luck!
Ok, not sure what exactly the problem was, but it was somewhere in the searchResults and/or xmlString method. searchResults object is originally created from XML received from the server (XML is parsed into the object structure). When xmlString was called, for some reason the string I was getting back was different from the original XML (I'm not talking about formatting, of course) - of 200,000 char-long string, within the first 500 chars or so there were some differences. I haven't been able to figure out why. So, instead of recreating the xml from object structure, I instead stored the original XML in a field in that object and, when xmlString was called, simply returned the original string. Now everything worked fine.
Thank you all for your support through this painful process.
Hi folks hope you can help. I'm attempting to create an anagram checker for an iPhone application. What I want to do is to be able to take a long string of maximum 81 letters and then check this against a word list to find all possible permutations of any length.
I've figured out how to do this on the simulator, but when I run this on the iPhone it is extremely slow, taking around 90 seconds to loop through the entire db (about 110000 rows). I checked Instruments and it doesn't show any memory leaks. However when I use Object Allocations it is clear that running the query creates a massive allocation for CFString that drains everything else. This immediately runs up 3.09 MB under the overall bytes column.
(In the code below, I've stripped out all of the anagram checking code as I wanted to identify what was causing the problem. So all this does at present is loop through the db without any output).
//create query
NSString *querySQL2 = #"SELECT name FROM table ";
sqlite3_stmt *statement2;
const char *query_stmt2 = [querySQL2 UTF8String];
sqlite3_prepare_v2(contactDB, query_stmt2, -1, &statement2, NULL);
//loop through all rows of database
while (sqlite3_step(statement2) == SQLITE_ROW)
{
NSString *laststring = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement2, 0)];
[laststring release];
}
sqlite3_finalize(statement2);
sqlite3_close(contactDB);
}
It seems obvious to me that the creation of 'laststring' below is what is sucking up all the memory. So why is it that when I put [laststring release]; at the end of the while loop it appears to have no effect? I've run this code with and without releasing and the same quantity of memory is used up. I've also tried wrapping an autorelease around it and this also had no effect.
I've read several other queries on looping through SQLite. Some of them suggested indexing but I'm not sure this will save me significant amounts of time with this problem. Also if I am searching all possible permutations from a large string of 81 letters, I'm guessing that at least 50% of the word list will need to be checked through anyway.
Any suggestions on how to keep CFString down?
Many thanks
Dave
Why do you alloc the NSString inside the while() loop?
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
NSString *laststring = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement2, 0)];
// do something with laststring
}
would suffice wouldn't it?
I am getting a request data and putting it in NSString. after that, I am getting the following string,
_VIEWSTATE=%2FwEPDwULLTE1MjI5NTA5MzBkZBfGgdOVhk6K8WsSgq64ngCpAncw&_EVENTVALIDATION=%2FwEWBgKdhbkbAvOQn7cGApKGyNkMAsKL2t4DApSbgLYHArS6otoP2nSQkmm0E6zJe2u91W5ntimqJ18%3D&x-rim-queue-id=MyOfflineQueue&form_id=723&txt2=siddhesh.b.chavan%40gmail.com&btnSubmit=Submit&x-rim1-request1-title=SignatureShouldBeDoneHere&x-rim-request-title=iPhone3+4%2F6%2F2011+3%3A57%3A21+AM
The thing I have to ask is, I want to get the "form_id" from this string which is "723".
so, How do I get that??
I want to get the form_ID for a request everytime. So, kindly help me out of this.
Thanking you.
These options are more intended for URL arguments parsing, which you are trying to do.
1. Range search : for a word, a character
most efficient but can be fastidious to write... (and read!)
See rangeOfString: and its friends on NSString documentation
2. Split
quick and elegant to write, but not so efficient
Since it is a URL argument style string, it is easily parsable by splitting on & and = witch can be done easily using componentsSeparatedByString: or componentsSeparatedByCharactersInSet:
3. Regular expressions
clean code, powerful
Regular expressions are imho the best choice to manipulate text, but they can be harder to use/learn than previous solutions. To use regular expressions I suggest two options:
iPhone OS >= 3.2 has regular expressions :
NSString rangeOfString:options:NSRegularExpressionSearch
But this is close to option 1.
RegexKitLit, with provides an excellent regular expression engine on OSX/iOS would provide, imho, the best and most powerful solution to your problem (and many others!!!)...
4. Other Kits/API/SDK
the missing api/toolkit/sdk? don't write code thousands people already wrote...
I wished that NSURL would support URL arguments, for parsing and build urls... but it does not.
I don't know a good URL parsing/toolbox library that offers such URL Arguments manipulation tools (Google Toolbox does not provide such URL arguments tools except for escaping which is already really useful) but I'm sure that exists! And a good library, with tested and reliable code would be for sure your best solution...
5. Others... there are many
I forgot to mention NSScanner which I never really looked into (bad me)
More generally, Apple documentation on this topic is interesting.
if you would like to try this: Let me know if it works.
NSInteger formId;
//separate the whole string first by "&" characters
NSArray *array = [myString componentsSeparatedByString:#"&"];
for (NSString *queryString in array)
{
//for every separated string, look if you have the "form_id" key
NSRange range = [queryString rangeOfString:#"form_id=" options:NSLiteralSearch];
if (range.location != NSNotFound)
{
NSLog(#"form_id query string does exist");
//form_id= has 7 characters, pick the character which comes after the "=" sign.
NSString *value = [queryString substringFromIndex:8];
formId = [value integerValue];
}
}
That solution is assuming that you only have 1 "form_id".
Try this :
[myString substringToIndex:index];
[myString substringFromIndex:index];
[myString substringWithRange:range];
Or
if ( [yourString rangeOfString:#"form_id"].location == NSNotFound )
{
NSLog(#"form_id not found");
}
Use 'rangeOfString:options:range:' start by searching the entire string for "form_id=" and then search from where that was found to for an "&". You will need to handle the case where the ampersand isnt found (when form_id is the last in the list).
You can use NSScanner object too. Like-
NSScanner *scanner = [NSScanner scannerWithString:reqData];
[scanner scanUpToString:#"form_id" intoString:nil];
[scanner scanUpToString:#"&" intoString:&strObj];
valueStr = [strObj substringFromIndex:1];
How do I use comma-separated-values received from a URL query in Objective-c?
when I query the URL I get csv such as ("OMRUAH=X",20.741,"3/16/2010","1:52pm",20.7226,20.7594).
How do I capture and use this for my application?
You have two options:
Use a CSV parser: http://freshmeat.net/projects/ccsvparse
Or parse the data yourself into an array:
// myString is an NSString object containing your data
NSArray *array = [myString componentsSeparatedByString: #","];
I recently dealt with CSV parsing for Yahoo! Finance as well. I used Ragel to write a parser in C that was good enough for the CSV I was getting. It handled everything but escaped quotes, which are not going to show up much in stock quotes. It was pretty painless and a good learning experience. I'd post the code, but it was work-for-hire, so I don't own it.
Turning a C string into an NSString is easy. If you have it as an NSData, as you likely do at the end of a URL download, just do [[NSString alloc] initWithData:csvData encoding:NSUTF8StringEncoding]. If you have a pointer to a character buffer instead, use [[NSString alloc] initWithBytes:buffer length:buflen encoding:NSUTF8StringEncoding]. buflen could be strlen(buffer) if buffer is a normal, NUL-terminated C string.