Cannot form weak reference to instance of class NSTextView
Use @IBOutlet var scrollView: NSScrollView
instead of @IBOutlet var textField: NSTextView
.
Then create a property returns documentView in scrollView.
import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet var window: NSWindow
@IBOutlet var scrollView: NSScrollView
var textField: NSTextView {
get {
return scrollView.contentView.documentView as NSTextView
}
}
@IBAction func displaySomeText(AnyObject) {
textField.insertText("A string...")
}
func applicationDidFinishLaunching(aNotification: NSNotification?) {
// Insert code here to initialize your application
}
func applicationWillTerminate(aNotification: NSNotification?) {
// Insert code here to tear down your application
}
}
Which iOS classes that don't support zeroing weak references?
If you try to form a weak reference to an object that doesn't support weak references, the program should die immediately. This is mentioned in the Objective-C Advancements in Depth video from WWDC 2011. So you should know immediately if you find a class that doesn't support them.
I'm pretty sure the lack of mention in Transitioning to ARC Release Notes means that all UIKit
classes are safe. I'm not sure if the warning about AV Foundation
classes applies to iOS or not. I tested creating a weak reference to AVCaptureSession
on both iOS 5 and Lion and neither crashed. I tested creating a weak reference to an NSWindow
on Lion and it crashed with the message cannot form weak reference to instance (0x102232ef0) of class NSWindow
.
weak property for delegate cannot be formed
According to Apple's Transitioning to ARC Release Notes,
You cannot currently create weak references to instances of the following classes:
NSATSTypesetter
,NSColorSpace
,NSFont
,NSMenuView
,NSParagraphStyle
,NSSimpleHorizontalTypesetter
, andNSTextView
.Note: In addition, in OS X v10.7, you cannot create weak references to instances of
NSFontManager
,NSFontPanel
,NSImage
,NSTableCellView
,NSViewController
,NSWindow
, andNSWindowController
. In addition, in OS X v10.7 no classes in the AV Foundation framework support weak references.
(Note: one needs to be very careful with nonzeroing weak references...)
unsafe_unretained vs weak : crash during setDelegate
I found the answer: as said in weak property for delegate cannot be formed
You cannot currently create zeroing-weak references to instances of the following classes (emphasis mine):
NSATSTypesetter, NSColorSpace, NSFont, NSFontManager, NSFontPanel, NSImage, NSMenuView, NSParagraphStyle, NSSimpleHorizontalTypesetter, NSTableCellView, NSTextView, NSViewController, NSWindow, and NSWindowController. In addition, in OS X no classes in the AV Foundation framework support weak references.
For declared properties, you should use assign instead of weak; for variables you should use __unsafe_unretained instead of __weak.
In addition, you cannot create weak references from instances of NSHashTable, NSMapTable, or NSPointerArray under ARC.
Strong and weak references in Swift
Straight from the Swift Language guide:
class Person {
let name: String
init(name: String) { self.name = name }
var apartment: Apartment?
deinit { println("\(name) is being deinitialized") }
}
class Apartment {
let number: Int
init(number: Int) { self.number = number }
weak var tenant: Person?
deinit { println("Apartment #\(number) is being deinitialized") }
}
properties are strong by default. But look at the tenant property of the class "Apartment", it is declared as weak. You can also use the unowned keyword, which translates to unsafe_unretained from Objective-C
https://itunes.apple.com/tr/book/swift-programming-language/id881256329?mt=11
Related Topics
Checking If a Swift Class Conforms to a Protocol and Implements an Optional Function
How to Get Class Name of Parent Viewcontroller in Swift
Format String with Trailing Zeros Removed for X Decimal Places in Swift
Sliding One Swiftui View Out from Underneath Another
Giving Physics to Tiles of Sktilemapnode in Xcode 8
React Native Sending Events to JavaScript in Swift
Kvo with Shared Nsuserdefaults in Swift
Swift: Force Show Navigation Bar in Modal
How to Fix ' *Pod* Does Not Support Provisioning Profiles' in Azure Devops Build Agent
Confusion Regarding Overriding Class Properties in Swift
Change Default Global Tint Color in Swift
Delegate Using Container View in Swift
How to Initialise Cvpixelbufferref in Swift
Swift: Switch Between Nsviewcontroller Inside Container View/Nsview
How to Call a Static Function on a Protocol in a Generic Way