how to catch exception thrown by imageWithData: iPhone - iphone

I want to try to create an image using NSdata. If imageWithData: method of UIImage can create it successfully, i will follow a path but if it cannot create i want to follow another way.
Is this even possible?
I tried
#try {
im = [UIImage imageWithData:data];
NSLog(#"Trying");
}
#catch (NSException * e) {
NSLog(#"Exception");
anotherData = doSomethingWithData(data)
im = [UIImage imageWithData:anotherData];
}
#finally {
NSLog(#"Final");
[self.questionList addObject:im];
}
but it causes app to crash.
How can I catch this exception without causing app to crash?
Exception is this:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0'
Code does not create image from data but still it does not throws an exception or anything about it. Is there a way that I can understand if image is created or not.

You get the error, because im remains nil and you are not allowed to add nil to an array.
You could do it like that:
im = [UIImage imageWithData:data];
if(im == nil) {
anotherData = doSomethingWithData(data)
im = [UIImage imageWithData:anotherData];
}
if(im != nil)
[self.questionList addObject:im];

An implementation similar to your pattern above would be to re-write the finally block like such:
#finally {
NSLog(#"Final");
if(im) {
[self.questionList addObject:im];
}
}
However the entire piece of code could easily be re-written (and should) to not require exception handling.

Related

Exception while setting property in subclass of MKMapView

I got a strange error. After I installed my test app in release mode I got an error:
[SomeMapView setRotateEnabled:]: unrecognized selector sent to instance 0x1c58ac40
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SomeMapView setRotateEnabled:]: unrecognized selector sent to instance 0x1c58ac40'
The Mapview was initialized and in the init method I was trying to deactivate the rotation. In the debugging mode it worked fine.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setRotateEnabled:FALSE];
[self initDelegate];
}
return self;
}
Maybe someone knows what's going on? Thanks in advance.
It was a problem in the iOS version. RotateEnabled in available only in iOS7 like Suresh point out. I had to check if the property was there (or check if it has iOS7) like
if ([self respondsToSelector:NSSelectorFromString(elementName)])
{
[self setValue:elementInnerText forKey:elementName];
}

App Crash due to FB sdk 3.2

I am using new FB IOS SDK and app crash and get below error
Assertion failure in -FBCacheIndex
_updateEntryInDatabaseForKey:entry:, /Users/chrisp/src/ios-sdk/src/FBCacheIndex.m Terminating app due to
uncaught exception 'NSInternalInconsistencyException', reason: ''
* First throw call stack: (0x334072a3 0x3b0a197f 0x3340715d 0x33cdcb13 0x10b3b1 0x10b635 0x10a7bf 0x3b4b911f 0x3b4b899b 0x3b4b8895
0x3b4c7215 0x3b4c73b9 0x3b4eda11 0x3b4ed8a4)
I have made google and found solution like -lsqlite3.0 in other linker flag and add -lsqlite3 framework, FBsession missing. and i have done all but still i am getting same issue.
My code as below:
in FBClass.m which is NSObject type
-(void) getFriendListForInvitation
{
# try {
if (self.friendPickerController == nil) {
// Create friend picker, and get data loaded into it.
self.friendPickerController = [[FBFriendPickerViewController alloc] init];
self.friendPickerController.title = #"Invite Friends";
self.friendPickerController.delegate = self;
[self.friendPickerController clearSelection];
[self.friendPickerController loadData];
self.friendPickerController.itemPicturesEnabled = TRUE;
}
else{
[self.friendPickerController clearSelection];
[self.friendPickerController updateView];
}
self.friendPickerController.allowsMultipleSelection = FALSE;
// iOS 5.0+ apps should use [UIViewController presentViewController:animated:completion:]
// rather than this deprecated method, but we want our samples to run on iOS 4.x as well.
if([[[UIDevice currentDevice] systemVersion] intValue] >= 5.0)
[[[appdelegate.navigationController viewControllers] lastObject] presentViewController:self.friendPickerController animated:TRUE completion:nil];
else
[[[appdelegate.navigationController viewControllers] lastObject] presentModalViewController:self.friendPickerController animated:YES];
}
#catch (NSException *exception) {
NSLog(#"%#",[exception description]);
}
}
Please help me to solve this issue.....
thanks
Did you manage to solve the issue? I suffer from the same problem.
I determined that this exception is caused by the missing file cache.db in the directory facebook is addressing to. You can check that with the help of the simulator. The directory of the missing simulator file is:
/Users/<user name>/Library/Application Support/iPhone Simulator/6.1/Applications/<app id>/Library/Caches/DataDiskCache
Yet I do not know why the file is missing.
EDIT:
The only thing I came up with was to clear the session data from the app with simple two lines of code:
FBSession.activeSession = nil;
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"FBAccessTokenInformationKey"];
Unfortunately this solution never actually closes the session but at least it is providing the proper workflow of the app.

Determine if an object is being accessed by more than one thread?

I'm getting sporadic EXC_BAD_ACCESS crashes which I'm thinking has to do with multi-threading issues. (I tried profiling with Zombies, but the app doesn't crash when profiling). So I'm wondering if there is any sort of mechanism, for debugging purposes, to determine if an object is being simultaneously accessed by more than one thread? Maybe somehow print a log statement if that is the case?
A simple and dirty method of telling if you are the only one executing on a thread would rely on unguarded static variables:
-(void)concurrentMethod {
static NSThread *runningThread = nil;
NSThread *myThread = [NSThread currentThread];
if (runningThread != nil) {
NSLog(#"Thread %#: running concurrently with %#", runningThread, myThread);
}
runningThread = myThread;
... // Do the useful stuff here
if (runningThread != myThread) {
NSLog(#"Thread %#: pre-empted by %#", myThread, runningThread);
}
runningThread = nil;
}

#catch not catching

In the code below I'm trying to catch an exception that is being thrown because the delegate doesn't implement the boolEntryCellViewControllerSegmentChanged method. However, the exception doesn't seem to be getting caught, the program is still crashing with an "unrecognized selector sent to instance" error. I'd be grateful for any pointers as to what I'm doing wrong.
#try
{
[delegate boolEntryCellViewControllerSegmentChanged:self];
}
#catch (NSException *exception)
{
NSLog(#"why doesn't this work?");
}
#finally
{
NSLog(#"why, why why?");
}
This is probably due to a known bug: http://www.openradar.me/8081169
In most cases, this only happens on the simulator. On the device itself, it usually works. But YMMV.

how to get local top 10 highscore from openfeint

this is my coding am used to get local top 10 highscore but , debugging terminated error occurs.
[OFHighScoreService getPageWithLoggedInUserForLeaderboard: theLeaderboardID onSuccess:OFDelegate(self, #selector(_scoresDownloaded:))
onFailure:OFDelegate()];
selector:-
- (void)_scoresDownloaded:(OFPaginatedSeries*)page
{
NSMutableArray* highscores = nil;
if ([page count] > 0)
{
if ([[page objectAtIndex:0] isKindOfClass:[OFTableSectionDescription class]])
{
highscores = [(OFTableSectionDescription*)[page objectAtIndex:0] page].objects;
}
else
{
highscores = page.objects;
}
}
NSString *userID = [OpenFeint lastLoggedInUserName];
for (OFHighScore* score in highscores)
{
ccColor3B theColor = ccBLACK;
if ([score.user.name isEqualToString: userID] ) {
//score now contains the users data... Do what I want with it.
NSLog(#"%d %# %d", score.rank, score.user.name, score.score);
break;
}
}
}
this is my console window error:-
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Levelone canReceiveCallbacksNow]: unrecognized selector sent to instance 0x6af2070'
*** Call stack at first throw:
terminate called after throwing an instance of 'NSException'
As the error says, the object you are using as the callback delegate for OFHighScoreService does not recognize the selector canReceiveCallbacksNow. As per OpenFeint documentation, your callback must implement the OFCallbackable protocol which defines this. Simply implement the function, e.g. just have it return YES all the time.
OpenFeint only stores the latest qualifying score per player on a given leaderboard. No user would ever appear ranked at more than one slot on a leaderboard.