I'm trying to programmatically add an NSDrawer to my app's main window (which also has an NSOutlineViewwhich was setup in IB):
Ivar:
var dd: NSDrawer? = nil
Then, when a disclosure triangle button is clicked:
if dd == nil {
var drawer_rect = NSInsetRect(self.window.frame, 30, 30).size
drawer_rect.height = 150
dd = NSDrawer.init(contentSize: drawer_rect, preferredEdge: NSRectEdge.minY)
dd!.contentView = self.status_scroll
dd!.parentWindow = self.window
}
So when the user tries to 'disclose' the drawer, the above code creates an NSDrawer and then displays it. It all works fine but Xcode dumps the following out as the parentWindow is set:
[General] ERROR: Setting <NSOutlineView: 0x100f0b9b0> as the first responder for window <NSDrawerWindow: 0x100fc8900>, but it is in a different window (<NSWindow: 0x6080001e0600>)! This would eventually crash when the view is freed. The first responder will be set to nil.
(
0 AppKit 0x00007fff9fc289cf -[NSWindow _validateFirstResponder:] + 557
1 AppKit 0x00007fff9f3a374c -[NSWindow _setFirstResponder:] + 31
2 AppKit 0x00007fff9f90c35b -[NSDrawerWindow _setParentWindow:] + 64
3 AppKit 0x00007fff9f90b666 -[NSDrawer(DrawerInternals) _doSetParentWindow:] + 382
4 AppKit 0x00007fff9f907786 -[NSDrawer setParentWindow:] + 78
Comment out the setting of the parentWindow and nothing gets dumped to the console.
NSDrawer is deprecated as per Apple documentation. You should consider a different design. If you still use NSDrawer, you may face such issues.
Related
By using a NSSplitViewController inside my Storyboard, I wanted to override the behaviour of the contained split view.
By implementing some of the the NSSplitViewDelegate method regarding the max constraint for the slider, I got this exception raised:
2017-01-12 11:05:45.873814 iCache[3399:659731] [General] SplitViewController's splitView is unable to use autolayout because the SplitViewController overrides an incompatible delegate method.
2017-01-12 11:05:45.874089 iCache[3399:659731] [General] (
0 CoreFoundation 0x00007fffb5d9ee7b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffca989cad objc_exception_throw + 48
2 CoreFoundation 0x00007fffb5da3b82 +[NSException raise:format:arguments:] + 98
3 Foundation 0x00007fffb77edd50 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 AppKit 0x00007fffb389ca29 -[NSSplitView _splitViewUseConstraintBasedLayout] + 355
5 AppKit 0x00007fffb389c894 -[NSSplitView(NSSplitViewDividerViews) _canUseDividerViewsAsSubviews] + 74
6 AppKit 0x00007fffb389c0f7 -[NSSplitView(NSSplitViewDividerViews) _updateDividerViews] + 36
7 AppKit 0x00007fffb389dd41 -[NSSplitViewController _updateSplitView:withBlock:] + 51
8 AppKit 0x00007fffb389dc89 -[NSSplitViewController viewDidLoad] + 144
9 AppKit 0x00007fffb3896283 -[NSViewController _sendViewDidLoad] + 97
10 CoreFoundation 0x00007fffb5d17889 -[NSSet makeObjectsPerformSelector:] + 217
11 AppKit 0x00007fffb3814902 -[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 1389
12 AppKit 0x00007fffb391d436 -[NSNib _instantiateNibWithExternalNameTable:options:] + 696
13 AppKit 0x00007fffb391d06a -[NSNib _instantiateWithOwner:options:topLevelObjects:] + 143
14 AppKit 0x00007fffb403f34a -[NSStoryboard instantiateControllerWithIdentifier:] + 234
15 AppKit 0x00007fffb3805bb7 NSApplicationMain + 780
16 iCache 0x00000001000127f4 main + 84
17 libdyld.dylib 0x00007fffcb26d255 start + 1
)
Apparently this is due to to Autolayout used for the split view.
Is there a way to disable Autolayout for this NSSplitView inside Interface Builder?
Enable/disable Auto Layout
You can disable Auto Layout per XIB file in the Interface Builder under File Inspector.
You should be able to control autolayout per view through the views translatesAutoresizingMaskIntoConstraints.
Why it doesn't work
According to OS X 10.11 release notes there are some methods you cannot use in combination with autolayout.
Please see this excerption from the notes.
Auto Layout NSSplitView improvements
In 10.8, NSSplitView properly respects constraints applied to its subviews, such as their minimum view widths. There are also new APIs for controlling the holding priorities, which determine both the NSLayoutPriority at which a split view holds its sizes and also which views change size if the split view itself grows or shrinks.
(NSLayoutPriority)holdingPriorityForSubviewAtIndex:(NSInteger)subviewIndex;
(void)setHoldingPriority:(NSLayoutPriority)priority forSubviewAtIndex:(NSInteger)subviewIndex;
In order to take advantage of these improvements, you must NOT implement any of the following NSSplitViewDelegate methods:
splitView:constrainMinCoordinate:ofSubviewAt:
splitView:constrainMaxCoordinate:ofSubviewAt:
splitView:resizeSubviewsWithOldSize:
splitView:shouldAdjustSizeOfSubview:
These methods are incompatible with auto layout. You can typically achieve their effects and more with auto layout.
I'm developing a Mac app using Swift 2 and Xcode 7.
I'm just getting started with Swift after many years focused on Ruby with a touch of JavaScript.
The Mac app that I'm creating includes a web browser view.
My ViewController.swift says:
import Cocoa
import WebKit
class ViewController: NSViewController {
#IBOutlet var url: NSTextField!
#IBOutlet var browser: WKWebView!
#IBAction func go(sender: AnyObject) {
// browser.loadRequest(NSURLRequest(URL:NSURL(string: url.stringValue)!))
// above line broken down for debugging
let url1 = url.stringValue
print("url1 = \(url1)")
let url2 = NSURL(string: url1)!
print("url2 = \(url2)")
let url3 = NSURLRequest(URL:url2)
print("url3 = \(url3)")
print("browser is \(browser)")
browser.loadRequest(url3)
}
}
The app builds successfully. When I run it, enter http://apple.com into the URL field and click the Go button, I see:
url1 = http://apple.com
url2 = http://apple.com
url3 = <NSURLRequest: 0x600000001400> { URL: http://apple.com }
browser is <WebView: 0x608000120e60>
2016-04-06 13:39:51.664 Testivate[6516:427535] -[WebView loadRequest:]: unrecognized selector sent to instance 0x608000120e60
2016-04-06 13:39:51.664 Testivate[6516:427535] -[WebView loadRequest:]: unrecognized selector sent to instance 0x608000120e60
2016-04-06 13:39:51.667 Testivate[6516:427535] (
0 CoreFoundation 0x00007fff93f4a4f2 __exceptionPreprocess + 178
1 libobjc.A.dylib 0x00007fff8ca7a73c objc_exception_throw + 48
2 CoreFoundation 0x00007fff93fb41ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00007fff93eba571 ___forwarding___ + 1009
4 CoreFoundation 0x00007fff93eba0f8 _CF_forwarding_prep_0 + 120
5 Testivate 0x000000010000271a _TFC9Testivate14ViewController2gofPs9AnyObject_T_ + 2154
6 Testivate 0x00000001000027b6 _TToFC9Testivate14ViewController2gofPs9AnyObject_T_ + 54
7 libsystem_trace.dylib 0x00007fff96e1807a _os_activity_initiate + 75
8 AppKit 0x00007fff94567e89 -[NSApplication sendAction:to:from:] + 460
9 AppKit 0x00007fff94579fde -[NSControl sendAction:to:] + 86
10 AppKit 0x00007fff94579f08 __26-[NSCell _sendActionFrom:]_block_invoke + 131
11 libsystem_trace.dylib 0x00007fff96e1807a _os_activity_initiate + 75
12 AppKit 0x00007fff94579e65 -[NSCell _sendActionFrom:] + 144
13 libsystem_trace.dylib 0x00007fff96e1807a _os_activity_initiate + 75
14 AppKit 0x00007fff9457848a -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2693
15 AppKit 0x00007fff945c0fd0 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 744
16 AppKit 0x00007fff94576bb4 -[NSControl mouseDown:] + 669
17 AppKit 0x00007fff94acb469 -[NSWindow _handleMouseDownEvent:isDelayedEvent:] + 6322
18 AppKit 0x00007fff94acc44d -[NSWindow _reallySendEvent:isDelayedEvent:] + 212
19 AppKit 0x00007fff9450b63d -[NSWindow sendEvent:] + 517
20 AppKit 0x00007fff9448bb3c -[NSApplication sendEvent:] + 2540
21 AppKit 0x00007fff942f2ef6 -[NSApplication run] + 796
22 AppKit 0x00007fff942bc46c NSApplicationMain + 1176
23 Testivate 0x0000000100004f34 main + 84
24 libdyld.dylib 0x00007fff9099a5ad start + 1
25 ??? 0x0000000000000003 0x0 + 3
)
Initially I thought the error was saying that loadRequest was an unrecognized selector for browser, but you can see I've pulled apart the function and browser is clearly an instance of WebView, which definitely has loadRequest as a method. You can see from how I've pulled apart the method that I'm definitely providing the loadRequest method with the expected NSURLRequest object.
I'm now thinking that maybe this has something to do with being unable to find the WebView in Main.storyboard, but surely it should be able to? I've defined connections on it like this:
When you view Main.storyboard as source, it says:
<webView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Ocj-mD-woP">
<rect key="frame" x="62" y="20" width="406" height="211"/>
<webPreferences key="preferences" defaultFontSize="12" defaultFixedFontSize="12">
<nil key="identifier"/>
</webPreferences>
</webView>
Any thoughts? Thanks.
You're mixing up two different kinds of web views.
In your code, you're saying that browser is a WKWebView. That's the newer kind of web view, that was first available in OS X 10.10. It has a method named -loadView: in Objective-C, or loadView() in Swift.
#IBOutlet var browser: WKWebView!
However, when you run the code, the object is actually a WebView, because that's what you put in your storyboard. That's an older kind of web view. It doesn't have a method named -loadView: so you get an error when you try to call it.
browser is <WebView: 0x608000120e60>
-[WebView loadRequest:]: unrecognized selector sent to instance 0x608000120e60
(Read the documentation you linked to again. The WebView's main frame implements -loadRequest:, but WebView does not.)
Here are two possible fixes:
Easier: Fix your code to use WebView correctly, but leave the storyboard the same.
#IBOutlet var browser: WebView!
...
browser.mainFrame.loadRequest(url3)
Harder: Keep the code using WKWebView, but remove the WebView from your storyboard.
Unfortunately it's currently impossible to put a WKWebView in a storyboard, so you will have to write some code to set it up yourself. See the linked answer for an example of how to do it.
If you want to take advantage of the new features of WKWebView, you'll have to use the second alternative.
XCode 6 Beta 3 using Swift.
In my App I use CoreData. When I run my App in simulator, XCode pops up the debugger with a breakpoint set somewhere in the CoreData library (see screenshot). This happens on several CoreData functions, for example when inserting new records or fetching records from an entity. The breakpoint position is always the same.
This is extremely annoying. When my App fetches 10 records from an entity I have to push the continue program execution button 10 times.
Because this breakpoint is set somewhere in machine code, the breakpoint inspector does not show any breakpoints so I cannot delete it.
Does anyone know how to get rid of it?
Many thanks.
Edit:
backtrace-output:
(lldb) bt
* thread #1: tid = 0x1d68b0, 0x000000010a2f7fcd libswift_stdlib_core.dylibswift_dynamicCastClassUnconditional + 77, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
* frame #0: 0x000000010a2f7fcd libswift_stdlib_core.dylibswift_dynamicCastClassUnconditional + 77
frame #1: 0x000000010a0fbb85 GPS TrackGPS_Track.TrackListTableViewController.tableView (tableView=<unavailable>)(Swift.ImplicitlyUnwrappedOptional<ObjectiveC.UITableView>, cellForRowAtIndexPath : Swift.ImplicitlyUnwrappedOptional<ObjectiveC.NSIndexPath>) -> Swift.Optional<ObjectiveC.UITableViewCell> + 1125 at TrackListTableViewController.swift:53
frame #2: 0x000000010a0fc937 GPS Track#objc GPS_Track.TrackListTableViewController.tableView (GPS_Track.TrackListTableViewController)(Swift.ImplicitlyUnwrappedOptional, cellForRowAtIndexPath : Swift.ImplicitlyUnwrappedOptional) -> Swift.Optional + 87 at TrackListTableViewController.swift:0
frame #3: 0x000000010bc2f218 UIKit-[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
frame #4: 0x000000010bc0f340 UIKit-[UITableView _updateVisibleCellsNow:isRecursive:] + 2845
frame #5: 0x000000010bc24fea UIKit-[UITableView layoutSubviews] + 213
frame #6: 0x000000010bbb1ebd UIKit-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 519
frame #7: 0x000000010b9c9598 QuartzCore-[CALayer layoutSublayers] + 150
frame #8: 0x000000010b9be1be QuartzCoreCA::Layer::layout_if_needed(CA::Transaction*) + 380
frame #9: 0x000000010b9be02e QuartzCoreCA::Layer::layout_and_display_if_needed(CA::Transaction*) + 24
frame #10: 0x000000010b92cf16 QuartzCoreCA::Context::commit_transaction(CA::Transaction*) + 242
frame #11: 0x000000010b92e022 QuartzCoreCA::Transaction::commit() + 390
frame #12: 0x000000010b92e68d QuartzCoreCA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 89
frame #13: 0x000000010ab52927 CoreFoundation__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
frame #14: 0x000000010ab52880 CoreFoundation__CFRunLoopDoObservers + 368
frame #15: 0x000000010ab480d3 CoreFoundation__CFRunLoopRun + 1123
frame #16: 0x000000010ab47a06 CoreFoundationCFRunLoopRunSpecific + 470
frame #17: 0x000000010e9e9abf GraphicsServicesGSEventRunModal + 161
frame #18: 0x000000010bb39cf8 UIKitUIApplicationMain + 1282
frame #19: 0x000000010a0e6a5d GPS Tracktop_level_code + 77 at AppDelegate.swift:36
frame #20: 0x000000010a0e6a9a GPS Trackmain + 42 at AppDelegate.swift:0
frame #21: 0x000000010d2e7145 libdyld.dylib`start + 1
(lldb)
I tracked it down further: The problem only occurs when using custom object classes for the entities. Example:
// User class, defined in User.swift
class User: NSManagedObject {
#NSManaged var name: String
#NSManaged var firstname: String
}
// --------------
// code somewhere else
let users = moc.executeFetchRequest(fetchRequest, error: &error)
for object in users {
let user = object as User // <-- breakpoint fired here
println(user.name)
}
}
SOLUTION:
One need to make the custom object class visible to Objective C using the #objc directive:
// User class, defined in User.swift
#objc(User) // <-- required!
class User: NSManagedObject {
#NSManaged var name: String
#NSManaged var firstname: String
}
Thanks to all for your help!
Do you have an All Exception breakpoint set?
Contrary to Apple's best practices CoreData uses exceptions in the normal flow of control.
If you add exception breakpoints you may break in CoreData. The solution is to remove or disable the exception breakpoint.
While the above answers all are technically correct
#zisoft is right, however if you're using custom NSManagedObject classes then you should always use #objc(User), not just because of this break point.
#Zaph may work also, as you're essentially not listening and if indeed it's a bug it should stop it from appearing
However you may still hit this kind of break point if you're not type checking. I suspect it's a breakpoint in the beta 4 but will become a crash in the next beta.
I solved the issue in my code as I was hitting on returning the managed object received from insertNewObjectForEntityForName as AnyObject and later saying as myclass when I used it. Fine as I knew it was my class. but actualy should have done something like this
func createMyEntity() -> MyClass{
if let entity : MyClass = NSEntityDescription.insertNewObjectForEntityForName("MyClass", inManagedObjectContext: self.managedObjectContext) as? MyClass
{
return entity;
}
return nil;
}
Obviously that's just one example, but if you're the hitting the break point in other places then hopefully this a good reference.
Not withstanding this still may just be a bug in beta 4 of xcode, but this is safer anyway.
Update -- It also checking that your data model class name matches as it later through a warning here an this may also be why the breakpoint was hit.
I fixed my version of this problem based on info in https://devforums.apple.com/message/1016337#1016337
make the NSManagedObject-derived class and its relevant properties public
do not include its source file in the test target's Compile Sources
import the app target in the test file -- e.g., import MyApp in MyClassTests.swift
In your Core Data *.xcdatamodeld file, prefix your entity's class name with your app name. It should look something like this when you're done:
Thanks the last helped!!!
make the NSManagedObject-derived class and its relevant properties public
do not include its source file in the test target's Compile Sources
import the app target in the test file -- e.g., import MyApp in MyClassTests.swift
Following helped me !
If I undone one of this settings the breakpoint error comes again !
You have to set for your Entity-Model in your *Model.xcdatamodeld in property "Class" the same Name example: Name=Chat Class=Chat
You have to add Code #objc(yourClass) above your class
Pictures 1:
http://i.stack.imgur.com/xoxtu.png
Pictures 2:
http://i.stack.imgur.com/LkYYq.png
For anyone using Xcode 6.2
Make #objc changes to your class i.e #objc(className)
Next go to *.xcdatamodeld -> Configurations (Default if nothing specified) -> add class against entity
I want to be able to tell if my application is currently in a "modal" state.
I know that if it is in this state using Cocoa's functions, I could tell by checking where [[NSApplication sharedApplication] modalWindow] returns nil or not.
But it could also be in modal state using Carbon's functions (RunAppModalLoopForWindow etc), and then Cocoa's modalWindow does not tell us whether the application is modal.
Unfortunately, I don't have the choice to avoid Carbon as my app hosts old third party plugins which do use it.
Here's part of an example stack trace in a modal state due to carbon:
frame #12: 0x93ede739 CoreFoundation`__CFRunLoopRun + 1897
frame #13: 0x93eddd5a CoreFoundation`CFRunLoopRunSpecific + 394
frame #14: 0x93eddbbb CoreFoundation`CFRunLoopRunInMode + 123
frame #15: 0x930cee2d HIToolbox`RunCurrentEventLoopInMode + 259
frame #16: 0x930cebb2 HIToolbox`ReceiveNextEventCommon + 526
frame #17: 0x93119c4a HIToolbox`AcquireNextEventInMode + 75
frame #18: 0x93269aea HIToolbox`_AcquireNextEvent + 58
frame #19: 0x932585dc HIToolbox`_RunAppModalLoop + 168
frame #20: 0x932584ee HIToolbox`RunAppModalLoopForWindow + 130
I could trace the stack and see that _RunAppModalLoop is there, but I don't like this solution.
You can try checking the output from GetWindowModality([[NSApp keyWindow] windowRef], ...) and/or the same applied to the -mainWindow.
You can see if -[NSRunLoop currentMode] is NSDefaultRunLoopMode.
However, you might get a different answer with a question more specific to the problem you're trying to solve.
This can be done by enumerating all the app's windows and checking if they are modal by using GetWindowModality.
bool isAnyCarbonWindowModal()
{
for (
WindowRef win = GetFrontWindowOfClass(kAllWindowClasses, true);
win != nullptr;
win = GetNextWindowOfClass(win, kAllWindowClasses, true))
{
WindowModality modalKind;
WindowRef unavailableWindow;
GetWindowModality(win, &modalKind, &unavailableWindow);
if (kWindowModalityAppModal == modalKind)
return true;
}
return false;
}
I am working on some basic video compositions using AVMutableComposition - currently a video layer (AVMutableVideoComposition) with a text layer (CATextLayer).
It all looks ok but when I let it export via "AVMutableComposition exportAsynchronouslyWithCompletionHandler " it goes off and completes but returns this error:
CoreAnimation: warning, deleted thread with uncommitted CATransaction; created by:
0 QuartzCore 0x00007fff8a106959 _ZN2CA11Transaction4pushEv + 219
1 QuartzCore 0x00007fff8a106531 _ZN2CA11Transaction15ensure_implicitEv + 273
2 QuartzCore 0x00007fff8a10d66f _ZN2CA5Layer13thread_flags_EPNS_11TransactionE + 37
3 QuartzCore 0x00007fff8a10d5a7 _ZN2CA5Layer4markEPNS_11TransactionEjj + 79
4 QuartzCore 0x00007fff8a112cac _ZN2CA5Layer27contents_visibility_changedEPNS_11TransactionEb + 216
5 QuartzCore 0x00007fff8a112b65 _ZN2CA5Layer12mark_visibleEPNS_11TransactionEb + 261
6 QuartzCore 0x00007fff8a112b26 _ZN2CA5Layer12mark_visibleEPNS_11TransactionEb + 198
7 QuartzCore 0x00007fff8a112b26 _ZN2CA5Layer12mark_visibleEPNS_11TransactionEb + 198
8 QuartzCore 0x00007fff8a1128d1 _ZN2CA5Layer11set_visibleEj + 335
9 QuartzCore 0x00007fff8a1126b9 _ZN2CA7Context9set_layerEPKv + 75
10 MediaToolbox 0x00007fff857f155b FigCoreAnimationRendererInvalidate + 108
11 CoreFoundation 0x00007fff8ec763df CFRelease + 511
12 MediaToolbox 0x00007fff857d3a6b FigVideoCompositionProcessorInvalidate + 675
13 MediaToolbox 0x00007fff85791341 FigAssetWriterCreateWithURL + 18573
14 MediaToolbox 0x00007fff85791f7b FigAssetWriterCreateWithURL + 21703
15 CoreMediaAuthoringCrunchers 0x00000001046e2b99 AssetAudioSourcer_CreateInstance + 3865
I find that this goes away if I comment-out the following line - however the CATextLayer is not rendered:
videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
Any thoughts anyone?
Adam
You must ensure any UI drawing occurs on the main thread. The line you commented out probably does some drawing internally, so you want to make sure this code is executed on the main thread. A couple ways to do this is by using the function dispatch_async() or the methods performSelectorOnMainThread:withObject:waitUntilDone: or performSelectorOnMainThread:withObject:waitUntilDone:modes:
- (void) someMethod
{
// You may need to load a container object to pass as myCustomData whose
// contents you then access in myCustomDrawing: if the data isn't accessible
// as instance data.
[...]
// Perform all drawing/UI updates on the main thread.
[self performSelectorOnMainThread:#selector(myCustomDrawing:)
withObject:myCustomData
waitUntilDone:YES];
[...]
}
- (void) myCustomDrawing:(id)myCustomData
{
// Perform any drawing/UI updates here.
videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer
inLayer:parentLayer];
}
For a related post on the difference between dispatch_async() and performSelectorOnMainThread:withObjects:waitUntilDone: see Whats the difference between performSelectorOnMainThread and dispatch_async on main queue?