Getting error after execute NSMutableArray - objective-c

Here is the code. I tried to create a object which includes the name, size and image(get from the resource) and then I add that object into NSMutableArray in order to show those object in NSTableView. However, after I run the code, I did get the correct counts of object in NSMutableArray and correct information in each object(name,size,image), but it still gives me error. Could you guys help me. I am a new Mac developer. Thanks !!
Here is the code:
#implementation AppDelegate{
NSMutableArray* _tableContents;
}
-(void)awakeFromNib{
NSString* imagePath = #"USB.png";
NSString* mountPath = #"/Volumes";
_tableContents = [[NSMutableArray alloc]init];
for(mountPath in [[NSWorkspace sharedWorkspace]mountedLocalVolumePaths])
{
if(0==[mountPath rangeOfString:#"/Volumes/"].location)
{
NSString* USBpath = [[[NSWorkspace sharedWorkspace]mountedLocalVolumePaths]objectAtIndex:count];
//get the name of USB drive
NSString* nameOfUSB = [USBpath substringFromIndex:9];
//get the size of USB drive
NSNumber *volumeSize;
NSURL *mountPathPicked = [NSURL fileURLWithPath:mountPath];
if([mountPathPicked getResourceValue:&volumeSize forKey:NSURLVolumeTotalCapacityKey error:nil])
{
NSDictionary *obj = #{#"name":nameOfUSB,
#"image":[NSImage imageNamed:imagePath],
#"size":volumeSize
};
//after I call the following line, it will give me error !!
[_tableContents addObject:obj];
}
}
count ++;
}
}
//I also use the following two methods in order to show the name, and image in NSTableView
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
return [_tableCountents count];
}
-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSDictionary *flag = _tableContents[row];
NSString *identifier = [tableColumn identifier];//call different col we need identifier
if([identifier isEqualToString:#"MainCell"])
{
NSTableCellView *cellView = [tableView makeViewWithIdentifier:#"MainCell" owner:self];//give back specific cell named mainCell
[cellView.textField setStringValue:flag[#"name"]];
[cellView.imageView setImage:flag[#"image"]];
return cellView;
}
return nil;
}
There is no connection problems in GUI. And here is the error message:
2013-04-25 01:16:41.321 NAMEOFPROJECT[8243:303] An uncaught exception was raised
2013-04-25 01:16:41.322 NAMEOFPROJECT[8243:303] *** -[__NSArrayI objectAtIndex:]: index 7 beyond bounds [0 .. 3]
2013-04-25 01:16:41.329 NAMEOFPROJECT[8243:303] (
0 CoreFoundation 0x00007fff9147cb06 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff900c03f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff9142fb53 -[__NSArrayI objectAtIndex:] + 163
3 CDTOANYDRIVE 0x00000001000011d6 -[AppDelegate DetectingUSBDrives] + 710
4 CDTOANYDRIVE 0x0000000100000efb -[AppDelegate awakeFromNib] + 43
5 AppKit 0x00007fff899631a8 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1234
6 AppKit 0x00007fff89b79976 -[NSNib _instantiateNibWithExternalNameTable:] + 610
7 AppKit 0x00007fff89b7a41a -[NSNib instantiateNibWithExternalNameTable:] + 17
8 AppKit 0x00007fff89b262b4 -[NSTableRowData _unarchiveViewWithIdentifier:owner:] + 520
9 CDTOANYDRIVE 0x000000010000174b -[AppDelegate tableView:viewForTableColumn:row:] + 219
10 AppKit 0x00007fff89b279a2 -[NSTableRowData _addViewToRowView:atColumn:row:] + 324
11 AppKit 0x00007fff89b2766f -[NSTableRowData _addViewsToRowView:atRow:] + 151
12 AppKit 0x00007fff89b25c05 -[NSTableRowData _addRowViewForVisibleRow:withPriorView:] + 415
13 AppKit 0x00007fff89b2598a -[NSTableRowData _addRowViewForVisibleRow:withPriorRowIndex:inDictionary:withRowAnimation:] + 272
14 AppKit 0x00007fff89b24c59 -[NSTableRowData _unsafeUpdateVisibleRowEntries] + 740
15 AppKit 0x00007fff89b247f1 -[NSTableRowData updateVisibleRowViews] + 119
16 AppKit 0x00007fff89afc5d7 -[NSTableView layout] + 165
17 AppKit 0x00007fff89aafe95 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 112
18 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
19 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
20 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
21 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
22 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
23 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
24 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
25 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
26 AppKit 0x00007fff89aafd2e -[NSView layoutSubtreeIfNeeded] + 615
27 AppKit 0x00007fff89aab4dc -[NSWindow(NSConstraintBasedLayout) layoutIfNeeded] + 201
28 AppKit 0x00007fff899d96a8 -[NSView _layoutAtWindowLevelIfNeeded] + 99
29 AppKit 0x00007fff899d8fe6 -[NSView _sendViewWillDrawInRect:clipRootView:] + 87
30 AppKit 0x00007fff899a59b1 -[NSView displayIfNeeded] + 1044
31 AppKit 0x00007fff89a62a48 -[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 1377
32 AppKit 0x00007fff89a62068 -[NSWindow _doOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 940
33 AppKit 0x00007fff89a61c4f -[NSWindow orderWindow:relativeTo:] + 159
34 AppKit 0x00007fff89963266 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1424
35 AppKit 0x00007fff8994214d loadNib + 317
36 AppKit 0x00007fff89941679 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 219
37 AppKit 0x00007fff899414ae -[NSBundle(NSNibLoading) loadNibNamed:owner:topLevelObjects:] + 200
38 AppKit 0x00007fff8994128e +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 360
39 AppKit 0x00007fff8993da2f NSApplicationMain + 398
40 CDTOANYDRIVE 0x0000000100000e82 main + 34
41 libdyld.dylib 0x00007fff9385a7e1 start + 0
42 ??? 0x0000000000000003 0x0 + 3
)
And the above error shows twice !!

I have tried your code in a mini-app and It works fine. There are few things that I might highlight:
Your exception occurs in the method DetectingUSBDrives, for which we don't have any code. I'm assuming that you have pasted the code in the awakeFromNib method.
You are using count as an index. Is this varibale initialized at zero?
The fact that the error appears twice suggests that your cell views hold a reference to your app delegate, which means that awakeFromNib is called once for each view creation. This can create many problem since you are initializing _tableOfContents there. You might want to move all that code to the init method instead.

Related

The contentViewController ((null)) or contentViewController.view is nil

I get the following error and stacktrace however the view is not nil (as shown by the logging down right before it)
Stacktrace
2014-02-05 08:29:47.338 MenuTimer[74522:303] An uncaught exception was raised
2014-02-05 08:29:47.338 MenuTimer[74522:303] The contentViewController ((null)) or contentViewController.view is nil.
2014-02-05 08:29:47.340 MenuTimer[74522:303] (
0 CoreFoundation 0x00007fff8e39341c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff87b1ee75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8e3932cc +[NSException raise:format:] + 204
3 AppKit 0x00007fff8157e3da -[NSPopover showRelativeToRect:ofView:preferredEdge:] + 228
4 MenuTimer 0x0000000100002a72 -[MenuTimer onSetTime:] + 818
5 AppKit 0x00007fff814f83d0 -[NSApplication sendAction:to:from:] + 327
6 AppKit 0x00007fff81513348 -[NSMenuItem _corePerformAction] + 394
7 AppKit 0x00007fff81513084 -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] + 117
8 AppKit 0x00007fff815624dd -[NSMenu _internalPerformActionForItemAtIndex:] + 35
9 AppKit 0x00007fff81562359 -[NSCarbonMenuImpl _carbonCommandProcessEvent:handlerCallRef:] + 104
10 AppKit 0x00007fff81509176 NSSLMMenuEventHandler + 716
11 HIToolbox 0x00007fff8acff6d4 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 892
12 HIToolbox 0x00007fff8acfec87 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 385
13 HIToolbox 0x00007fff8ad12d90 SendEventToEventTarget + 40
14 HIToolbox 0x00007fff8ad48a30 _ZL18SendHICommandEventjPK9HICommandjjhPKvP20OpaqueEventTargetRefS5_PP14OpaqueEventRef + 420
15 HIToolbox 0x00007fff8ad7b618 SendMenuCommandWithContextAndModifiers + 59
16 HIToolbox 0x00007fff8ad7b5c4 SendMenuItemSelectedEvent + 178
17 HIToolbox 0x00007fff8ad7b4a5 _ZL19FinishMenuSelectionP13SelectionDataP10MenuResultS2_ + 94
18 HIToolbox 0x00007fff8ad5956e _ZL19PopUpMenuSelectCoreP8MenuData5PointdS1_tjPK4RecttjS4_S4_PK10__CFStringPP13OpaqueMenuRefPt + 1726
19 HIToolbox 0x00007fff8ad586c6 _HandlePopUpMenuSelection7 + 596
20 AppKit 0x00007fff81561221 _NSSLMPopUpCarbonMenu3 + 4153
21 AppKit 0x00007fff815601d6 _NSPopUpCarbonMenu3 + 39
22 AppKit 0x00007fff8155fe96 -[NSCarbonMenuImpl popUpMenu:atLocation:width:forView:withSelectedItem:withFont:withFlags:withOptions:] + 350
23 AppKit 0x00007fff81950959 +[NSStatusBarButtonCell popupStatusBarMenu:inRect:ofView:withEvent:] + 699
24 AppKit 0x00007fff81951ade -[NSStatusItem popUpStatusItemMenu:] + 173
25 MenuTimer 0x0000000100001779 -[StatusBarView mouseDown:] + 633
26 AppKit 0x00007fff814ded08 -[NSWindow sendEvent:] + 11296
27 AppKit 0x00007fff815da11b -[NSStatusBarWindow sendEvent:] + 247
28 AppKit 0x00007fff8147d744 -[NSApplication sendEvent:] + 2021
29 AppKit 0x00007fff812cda29 -[NSApplication run] + 646
30 MenuTimer 0x0000000100002500 -[MenuTimer init] + 2112
31 MenuTimer 0x00000001000031b5 main + 101
32 libdyld.dylib 0x00007fff87fcc5fd start + 1
)
However the following code and output indicates differently. Is it because I don't have an explicit controller?
Code
// create set time window
NSLog(#"Set Time...");
NSLog(#"View %#", self.statusTime.view);
NSLog(#"Frame %#", self.statusTime.view.frame);
NSPopover *popover = [[NSPopover alloc] init];
NSLog(#"Showing popover...");
[popover showRelativeToRect:[self.statusTime.view frame]
ofView:self.statusTime.view
preferredEdge:NSMinYEdge];
Output
2014-02-05 08:29:47.237 MenuTimer[74522:303] Set Time...
2014-02-05 08:29:47.237 MenuTimer[74522:303] View <StatusBarView: 0x6080001b05a0>
2014-02-05 08:29:47.238 MenuTimer[74522:303] Frame <StatusBarView: 0x6080001b05a0>
2014-02-05 08:29:47.238 MenuTimer[74522:303] Showing popover...
You need to set the contentViewController (what the popover is actually showing) before showing it.
i.e.
NSViewController *viewController; // some view controller created programmatically or from a nib
NSPopover *popover = [[NSPopover alloc] init];
popover.contentViewController = viewController; // Missing this.
NSLog(#"Showing popover...");
[popover showRelativeToRect:[self.statusTime.view frame]
ofView:self.statusTime.view
preferredEdge:NSMinYEdge];
The ofView: parameter is just where the popover is being shown from.

Why does [NSTextStorage setAttributedString] crash with NSMutableAttributedString?

When I run the following Code it crashes at the last line. I don't have any idea why. This function gets called in awakeFromNib.
- (void)setMotdText:(NSString *)text
{
NSString *boldFontName = [[NSFont boldSystemFontOfSize:12] fontName];
NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc] initWithString:text];
[attrstr beginEditing];
[attrstr addAttribute:NSFontAttributeName value:boldFontName range:NSMakeRange(0, 16)];
[attrstr endEditing];
[self.motdTextView.textStorage setAttributedString:attrstr];
}
I get this crash log:
2012-06-22 11:32:22.348 MSM-Plan[20785:403] -[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0x7fff79b9f380
2012-06-22 11:32:22.349 MSM-Plan[20785:403] An uncaught exception was raised
2012-06-22 11:32:22.349 MSM-Plan[20785:403] -[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0x7fff79b9f380
2012-06-22 11:32:22.351 MSM-Plan[20785:403] (
0 CoreFoundation 0x00007fff8a5bef56 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8ae19d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8a64b1be -[NSObject doesNotRecognizeSelector:] + 190
3 CoreFoundation 0x00007fff8a5abe23 ___forwarding___ + 371
4 CoreFoundation 0x00007fff8a5abc38 _CF_forwarding_prep_0 + 232
5 AppKit 0x00007fff8e0b060c -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixFontAttributeInRange:] + 1249
6 AppKit 0x00007fff8e0afeb7 -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixAttributesInRange:] + 64
7 AppKit 0x00007fff8e19b521 -[NSTextStorage processEditing] + 107
8 AppKit 0x00007fff8e0c2564 -[NSTextStorage edited:range:changeInLength:] + 385
9 Foundation 0x00007fff8d6f4497 -[NSConcreteMutableAttributedString replaceCharactersInRange:withAttributedString:] + 328
10 AppKit 0x00007fff8e1dfd19 -[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:] + 81
11 MSM-Plan 0x000000010206af5a -[MSM_PlanAppDelegate setMotdText:] + 314
12 MSM-Plan 0x000000010206c455 -[MSM_PlanAppDelegate awakeFromNib] + 1701
13 CoreFoundation 0x00007fff8a5b5fb1 -[NSObject performSelector:] + 49
14 CoreFoundation 0x00007fff8a5b5f32 -[NSSet makeObjectsPerformSelector:] + 274
15 AppKit 0x00007fff8e0369ff -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1245
16 AppKit 0x00007fff8e02cf73 loadNib + 322
17 AppKit 0x00007fff8e02c470 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 217
18 AppKit 0x00007fff8e02c38b +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] + 141
19 AppKit 0x00007fff8e02c2ce +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364
20 AppKit 0x00007fff8e29d06f NSApplicationMain + 398
21 MSM-Plan 0x000000010206adf2 main + 34
22 MSM-Plan 0x000000010206adc4 start + 52
)
2012-06-22 11:32:22.416 MSM-Plan[20785:403] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0x7fff79b9f380'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8a5bef56 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8ae19d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8a64b1be -[NSObject doesNotRecognizeSelector:] + 190
3 CoreFoundation 0x00007fff8a5abe23 ___forwarding___ + 371
4 CoreFoundation 0x00007fff8a5abc38 _CF_forwarding_prep_0 + 232
5 AppKit 0x00007fff8e0b060c -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixFontAttributeInRange:] + 1249
6 AppKit 0x00007fff8e0afeb7 -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixAttributesInRange:] + 64
7 AppKit 0x00007fff8e19b521 -[NSTextStorage processEditing] + 107
8 AppKit 0x00007fff8e0c2564 -[NSTextStorage edited:range:changeInLength:] + 385
9 Foundation 0x00007fff8d6f4497 -[NSConcreteMutableAttributedString replaceCharactersInRange:withAttributedString:] + 328
10 AppKit 0x00007fff8e1dfd19 -[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:] + 81
11 MSM-Plan 0x000000010206af5a -[MSM_PlanAppDelegate setMotdText:] + 314
12 MSM-Plan 0x000000010206c455 -[MSM_PlanAppDelegate awakeFromNib] + 1701
13 CoreFoundation 0x00007fff8a5b5fb1 -[NSObject performSelector:] + 49
14 CoreFoundation 0x00007fff8a5b5f32 -[NSSet makeObjectsPerformSelector:] + 274
15 AppKit 0x00007fff8e0369ff -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1245
16 AppKit 0x00007fff8e02cf73 loadNib + 322
17 AppKit 0x00007fff8e02c470 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 217
18 AppKit 0x00007fff8e02c38b +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] + 141
19 AppKit 0x00007fff8e02c2ce +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364
20 AppKit 0x00007fff8e29d06f NSApplicationMain + 398
21 MSM-Plan 0x000000010206adf2 main + 34
22 MSM-Plan 0x000000010206adc4 start + 52
)
terminate called throwing an exception(lldb)
Has anybody an idea? If I call it with a NSAttributedString-Object there´s no error.
I had to change the first line to:
NSFont *boldFont = [NSFont boldSystemFontOfSize:12];
Now it works, because the value attribute requires a NSFont-Object and not a NSString with the fonts name.
This is the working Code:
- (void)setMotdText:(NSString *)text
{
NSFont *boldFont = [NSFont boldSystemFontOfSize:12];
NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc] initWithString:text];
[attrstr beginEditing];
[attrstr addAttribute:NSFontAttributeName value:boldFont range:NSMakeRange(0, 16)];
[attrstr endEditing];
[self.motdTextView.textStorage setAttributedString:attrstr];
}

Get iTunes Artwork for Current Song with ScriptingBridge

I have been trying to figure out how to get the iTunes artwork for the currently playing song with scripting bridge. I have gotten to a point where it works for some songs, but for others, I get a SIGABRT. I'm not sure what the issue could be, so any help would be greatly appreciated. Here is what I have so far:
iTunesApplication * iTunes = [SBApplication applicationWithBundleIdentifier:#"com.apple.iTunes"];
NSImage *songArtwork;
iTunesTrack *current = [iTunes currentTrack];
iTunesArtwork *artwork = (iTunesArtwork *)[[[current artworks] get] lastObject];
if(artwork != nil)
songArtwork = [artwork data];
else
songArtwork = [NSImage imageNamed:#"Image.tiff"];
NSMenuItem *artworkMenuItem = [[NSMenuItem alloc] initWithTitle:#"" action:NULL keyEquivalent:#""];
[songArtwork setSize:NSMakeSize(128, 128)];
[artworkMenuItem setImage:songArtwork];
[Menu insertItem:artworkMenuItem atIndex:0];
I for some songs it works, and displays the artwork nicely in the menu item, but for others I get a SIGABRT on the line:
[songArtwork setSize:NSMakeSize(128, 128)];
The output of the console is as follows:
2011-08-12 23:13:20.094 SongViewer[2146:707] -[NSAppleEventDescriptor setSize:]: unrecognized selector sent to instance 0x102827f70
2011-08-12 23:13:20.095 SongViewer[2146:707] An uncaught exception was raised
2011-08-12 23:13:20.096 SongViewer[2146:707] -[NSAppleEventDescriptor setSize:]: unrecognized selector sent to instance 0x102827f70
2011-08-12 23:13:20.097 SongViewer[2146:707] (
0 CoreFoundation 0x00007fff86f11986 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8b04cd5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff86f9d5ae -[NSObject doesNotRecognizeSelector:] + 190
3 CoreFoundation 0x00007fff86efe803 ___forwarding___ + 371
4 CoreFoundation 0x00007fff86efe618 _CF_forwarding_prep_0 + 232
5 SongViewer 0x0000000100002a83 -[IPMenulet awakeFromNib] + 4483
6 CoreFoundation 0x00007fff86f089e1 -[NSObject performSelector:] + 49
7 CoreFoundation 0x00007fff86f08962 -[NSSet makeObjectsPerformSelector:] + 274
8 AppKit 0x00007fff8d9d9c27 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1245
9 AppKit 0x00007fff8d9d01b9 loadNib + 322
10 AppKit 0x00007fff8d9cf6b6 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 217
11 AppKit 0x00007fff8d9cf5d1 +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] + 141
12 AppKit 0x00007fff8d9cf514 +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364
13 AppKit 0x00007fff8dc42355 NSApplicationMain + 398
14 SongViewer 0x0000000100001882 main + 34
15 SongViewer 0x0000000100001854 start + 52
)
2011-08-12 23:13:20.098 SongViewer[2146:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSAppleEventDescriptor setSize:]: unrecognized selector sent to instance 0x102827f70'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff86f11986 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8b04cd5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff86f9d5ae -[NSObject doesNotRecognizeSelector:] + 190
3 CoreFoundation 0x00007fff86efe803 ___forwarding___ + 371
4 CoreFoundation 0x00007fff86efe618 _CF_forwarding_prep_0 + 232
5 SongViewer 0x0000000100002a83 -[IPMenulet awakeFromNib] + 4483
6 CoreFoundation 0x00007fff86f089e1 -[NSObject performSelector:] + 49
7 CoreFoundation 0x00007fff86f08962 -[NSSet makeObjectsPerformSelector:] + 274
8 AppKit 0x00007fff8d9d9c27 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1245
9 AppKit 0x00007fff8d9d01b9 loadNib + 322
10 AppKit 0x00007fff8d9cf6b6 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 217
11 AppKit 0x00007fff8d9cf5d1 +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] + 141
12 AppKit 0x00007fff8d9cf514 +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364
13 AppKit 0x00007fff8dc42355 NSApplicationMain + 398
14 SongViewer 0x0000000100001882 main + 34
15 SongViewer 0x0000000100001854 start + 52
)
terminate called throwing an exception(gdb)
If anyone has any idea what could be wrong, please let me know!!
Ok so I figured it out. The solution is to use the NSData raw data provided by the API rather than the NSImage. So I used:
NSImage *songArtwork = [[NSImage alloc] initWithData:[artwork rawData]];
rather than
songArtwork = [artwork data];
I have to fetch batch of track artworks from iTunes and use ‘rawData’ too.
But this way is inefficient.
I found a better way (actually it decreases ‘fetch’ time about 2 times, it’s very significant when fetching artworks for many tracks using ‘valueForKey:’ according to «Improving the Performance of Scripting Bridge Code»).
So I decided to understand what’s wrong with iTunesArtwork ‘data’ property.
We expect to get an NSImage object (according to iTunes.h ), but real object is kind of ‘NSAppleEventDescriptor’. And it’s easy to guess, that this object contains an image for us. So we can just use ‘data’ property to get image data. And this is really faster than getting ‘rawData’ from iTunesArtwork.
But sometimes iTunes return NSImage object instead NSAppleEventDescriptor. This is an strange behavior, but it's still faster then using rawData.

Simple document app, throwing an exception on "revert to saved" in Mac OS X 10.7 Lion

I have a simple document app, based on the document template, which is throwing an exception when the "Revert to Saved -> Last Opened Version" menu item is selected on Mac OS X 10.7.
The exception isn't in my code, it's inside Cocoa. It also doesn't appear to be related to anything I'm doing. My app is very simple (at this stage), almost a vanilla cocoa document based app, based on the template included with the latest stable version of Xcode.
I realise this is probably a bug in Lion, but I need to find a workaround.
See below for the exception, and the entire contents (very small) of my NSDocument subclass.
Steps to reproduce
open any document
type a single character into the text view
select File -> Revert to Saved
click Last Opened Version
Exception
2011-09-04 07:10:29.182 myapp[15433:707] *** -[NSPathStore2 stringByAppendingPathExtension:]: nil argument
2011-09-04 07:10:29.191 myapp[15433:707] (
0 CoreFoundation 0x00007fff89c2b986 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff90c6ed5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff89c2b7ba +[NSException raise:format:arguments:] + 106
3 CoreFoundation 0x00007fff89c2b744 +[NSException raise:format:] + 116
4 Foundation 0x00007fff86d2b172 -[NSPathStore2 stringByAppendingPathExtension:] + 112
5 AppKit 0x00007fff9148f8c3 -[NSDocument _preserveCurrentVersionForReason:error:] + 579
6 AppKit 0x00007fff9147655b __-[NSDocument _revertToVersion:preservingFirst:error:]_block_invoke_3 + 99
7 Foundation 0x00007fff86ef28c3 __-[NSFileCoordinator coordinateReadingItemAtURL:options:error:byAccessor:]_block_invoke_1 + 113
8 Foundation 0x00007fff86ef2f34 -[NSFileCoordinator(NSPrivate) _invokeAccessor:orDont:thenRelinquishAccessClaimForID:] + 202
9 Foundation 0x00007fff86d98a28 -[NSFileCoordinator(NSPrivate) _coordinateReadingItemAtURL:options:error:byAccessor:] + 663
10 Foundation 0x00007fff86ef284c -[NSFileCoordinator coordinateReadingItemAtURL:options:error:byAccessor:] + 79
11 AppKit 0x00007fff91484b41 __-[NSDocument _revertToVersion:preservingFirst:error:]_block_invoke_1 + 347
12 AppKit 0x00007fff914901e3 -[NSDocument performSynchronousFileAccessUsingBlock:] + 42
13 AppKit 0x00007fff9148fc90 -[NSDocument _revertToVersion:preservingFirst:error:] + 125
14 AppKit 0x00007fff91476cf9 -[NSDocument _revertToDiscardRecentChangesPreservingFirst:error:] + 43
15 AppKit 0x00007fff91477094 __-[NSDocument _revertToDiscardRecentChangesThenContinue:]_block_invoke_3 + 164
16 AppKit 0x00007fff91474851 __-[NSDocument performSynchronousFileAccessUsingBlock:]_block_invoke_1 + 19
17 AppKit 0x00007fff91475bda -[NSDocument continueFileAccessUsingBlock:] + 227
18 AppKit 0x00007fff91490413 -[NSDocument _performFileAccessOnMainThread:usingBlock:] + 466
19 AppKit 0x00007fff9149023f -[NSDocument performSynchronousFileAccessUsingBlock:] + 134
20 AppKit 0x00007fff91495891 __-[NSDocument _revertToDiscardRecentChangesThenContinue:]_block_invoke_2 + 301
21 AppKit 0x00007fff914909a9 -[NSDocument _something:wasPresentedWithResult:soContinue:] + 21
22 AppKit 0x00007fff91381bee -[NSAlert didEndAlert:returnCode:contextInfo:] + 93
23 AppKit 0x00007fff9138e356 -[NSApplication endSheet:returnCode:] + 275
24 AppKit 0x00007fff91381ab4 -[NSAlert buttonPressed:] + 265
25 CoreFoundation 0x00007fff89c1b11d -[NSObject performSelector:withObject:] + 61
26 AppKit 0x00007fff911dd852 -[NSApplication sendAction:to:from:] + 139
27 AppKit 0x00007fff911dd784 -[NSControl sendAction:to:] + 88
28 AppKit 0x00007fff911dd6af -[NSCell _sendActionFrom:] + 137
29 AppKit 0x00007fff911dcb7a -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2014
30 AppKit 0x00007fff9125c57c -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 489
31 AppKit 0x00007fff911db786 -[NSControl mouseDown:] + 786
32 AppKit 0x00007fff911a666e -[NSWindow sendEvent:] + 6280
33 AppKit 0x00007fff9113ef19 -[NSApplication sendEvent:] + 5665
34 AppKit 0x00007fff910d542b -[NSApplication run] + 548
35 AppKit 0x00007fff9135352a NSApplicationMain + 867
36 myapp 0x00000001000018d2 main + 34
37 myapp 0x00000001000018a4 start + 52
)
NSDocument Subclass
#implementation MyTextDocument
#synthesize textStorage;
#synthesize textView;
+ (BOOL)autosavesInPlace
{
return YES;
}
- (id)init
{
self = [super init];
if (self) {
self.textStorage = nil;
self.textView = nil;
textContentToLoad = [#"" retain];
}
return self;
}
- (NSString *)windowNibName
{
return #"MyTextDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
self.textStorage = self.textView.textStorage;
[self loadTextContentIntoStorage];
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
return [self.textStorage.string dataUsingEncoding:NSUTF8StringEncoding];
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
textContentToLoad = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (!textContentToLoad) {
*outError = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnknownError userInfo:nil];
return NO;
}
[self loadTextContentIntoStorage];
return YES;
}
- (void)loadTextContentIntoStorage
{
if (!self.textStorage || !textContentToLoad)
return;
[self.textStorage beginEditing];
[self.textStorage replaceCharactersInRange:NSMakeRange(0, self.textStorage.length) withString:textContentToLoad];
[textContentToLoad release], textContentToLoad = nil;
}
#end
Is your plist set up correctly when it comes to types?
It appears that NSDocument passes the result of [self fileNameExtensionForType:[self autosavingFileType] saveOperation:NSAutosaveElsewhereOperation] to stringByAppendingPathExtension: (the method that's throwing the exception here). If your app returns nil for this expression, then this exception may result.
You should probably file a bug at Apple for this, but in the meantime, make sure your app returns something non-nil.
Paste this into you document class (e.g. Document.m):
-(NSString*)fileNameExtensionForType:(NSString *)typeName saveOperation: (NSSaveOperationType)saveOperation
{
return #"yourAutosaveExtension";
}
Before, like kperryua said, this method was not implemented and returned nil.

xcode 'NSInvalidArgumentException', reason: '-[cure superview]: unrecognized selector sent to instance 0x4e3b7b0'

i am not sure why i am getting this error
i created a regular tab bar application
made 5 tabs (compiles and runs)
then put a tableview in one of the tabs , put the code below and i get this error
cure.h
#import <UIKit/UIKit.h>
#interface cure : UIViewController
<UITableViewDataSource, UITableViewDelegate>
{
NSArray *exercises;
}
#end
cure.m
#import "cure.h"
#implementation cure
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
// create a cell
if ( cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"cell"];
}
// set the main text
cell.textLabel.text = [exercises objectAtIndex:indexPath.row];
// return the cell.
return cell;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
exercises = [[NSArray alloc]
initWithObjects:#"An Interesting Title",
#"A Not That Interesting Title",
#"And Still Another Title", nil];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
in mainwindows.xib
i have the right tab class name "cure"
error
2011-04-29 22:22:06.446 week 3 week clear [4950:40b] -[cure superview]: unrecognized selector sent to instance 0x4e3b7b0
2011-04-29 22:22:06.451 peek 3 week clear [4950:40b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[cure superview]: unrecognized selector sent to instance 0x4e3b7b0'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc15a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f15313 objc_exception_throw + 44
2 CoreFoundation 0x00dc30bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d32966 ___forwarding___ + 966
4 CoreFoundation 0x00d32522 _CF_forwarding_prep_0 + 50
5 UIKit 0x002e2365 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 77
6 UIKit 0x002e0aa3 -[UIView(Hierarchy) addSubview:] + 57
7 UIKit 0x002e87c1 -[UIView initWithCoder:] + 840
8 Foundation 0x00017c24 _decodeObjectBinary + 3296
9 Foundation 0x00016d91 _decodeObject + 224
10 UIKit 0x004ac979 -[UIRuntimeConnection initWithCoder:] + 212
11 Foundation 0x00017c24 _decodeObjectBinary + 3296
12 Foundation 0x000189f5 -[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:] + 1354
13 Foundation 0x00019024 -[NSArray(NSArray) initWithCoder:] + 596
14 Foundation 0x00017c24 _decodeObjectBinary + 3296
15 Foundation 0x00016d91 _decodeObject + 224
16 UIKit 0x004abc36 -[UINib instantiateWithOwner:options:] + 804
17 UIKit 0x004adab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
18 UIKit 0x00363628 -[UIViewController _loadViewFromNibNamed:bundle:] + 70
19 UIKit 0x00361134 -[UIViewController loadView] + 120
20 UIKit 0x0036100e -[UIViewController view] + 56
21 UIKit 0x00373f54 -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 120
22 UIKit 0x00372aaa -[UITabBarController transitionFromViewController:toViewController:] + 64
23 UIKit 0x003748a2 -[UITabBarController _setSelectedViewController:] + 263
24 UIKit 0x00374711 -[UITabBarController _tabBarItemClicked:] + 352
25 UIKit 0x002b14fd -[UIApplication sendAction:to:from:forEvent:] + 119
26 UIKit 0x004b3ce6 -[UITabBar _sendAction:withEvent:] + 422
27 UIKit 0x002b14fd -[UIApplication sendAction:to:from:forEvent:] + 119
28 UIKit 0x00341799 -[UIControl sendAction:to:forEvent:] + 67
29 UIKit 0x00343c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
30 UIKit 0x00341750 -[UIControl sendActionsForControlEvents:] + 49
31 UIKit 0x002b14fd -[UIApplication sendAction:to:from:forEvent:] + 119
32 UIKit 0x00341799 -[UIControl sendAction:to:forEvent:] + 67
33 UIKit 0x00343c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
34 UIKit 0x003427d8 -[UIControl touchesEnded:withEvent:] + 458
35 UIKit 0x002d5ded -[UIWindow _sendTouchesForEvent:] + 567
36 UIKit 0x002b6c37 -[UIApplication sendEvent:] + 447
37 UIKit 0x002bbf2e _UIApplicationHandleEvent + 7576
38 GraphicsServices 0x01719992 PurpleEventCallback + 1550
39 CoreFoundation 0x00da2944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
40 CoreFoundation 0x00d02cf7 __CFRunLoopDoSource1 + 215
41 CoreFoundation 0x00cfff83 __CFRunLoopRun + 979
42 CoreFoundation 0x00cff840 CFRunLoopRunSpecific + 208
43 CoreFoundation 0x00cff761 CFRunLoopRunInMode + 97
44 GraphicsServices 0x017181c4 GSEventRunModal + 217
45 GraphicsServices 0x01718289 GSEventRun + 115
46 UIKit 0x002bfc93 UIApplicationMain + 1160
47 peek 3 week clear 0x00002658 main + 102
48 peek 3 week clear 0x000025e9 start + 53
)
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
what am i doing wrong?
thank you
You have probably assigned the "cure" class to a UIView in the "Identity" tab in Interface Builder. "cure" is a view controller, so it doesn't implement any UIView methods (such as -superview), and thus throws an exception when trying to load the nib.
Btw, you have a whole bunch of leaks in your code, please read something about memory management in Objective-C (perhaps also about Cocoa naming conventions).