Erratic error in SnapKit - swift

The following is my viewDidAppear() method with which I'm trying to prototype a feature for my app. The graphicsView instance variable is bound via the storyboard to an instance of a subclass of NSView I've written that in turn is enclosed in an NSScrollView within an NSSplitView. This code is within the view controller for that view.
override func viewWillAppear() {
super.viewWillAppear()
let red = CGColor.init(red: 1, green: 0, blue: 0, alpha: 1)
self.view.layer?.backgroundColor = red
let box = NSTextView()
self.graphicsView.addSubview(box)
box.snp.makeConstraints { (make) -> Void in
make.edges.equalTo(self.graphicsView).inset(NSEdgeInsetsMake(100, 100, self.graphicsView.bounds.height - 200, self.graphicsView.bounds.width - 300))
}
box.textStorage?.append(NSAttributedString(string: "Hello Sailor"))
box.alignCenter(self)
}
When executed, I get the error Cannot form weak reference to instance (0x6000001224e0) of class NSTextView. It is possible that this object was over-released, or is in the process of deallocation. along with the usual EXC_BAD_INSTRUCTION fault on the closing bracket of the trailing closure for the constraints.
As far as I can see, the NSTextView will be strongly retained by box, and so I'm at a loss to see the source of the error. The error shows up at the first line of ConstraintItem.init(target: AnyObject?, attributes: ConstraintAttributes). Per the instructions in the readme I'm posting here; can someone on the SnapKit team perhaps shed any additional light on the error? (The app works normally if I remove the box-related code.)
Added information:
The exception happens at line 37 of ConstraintItem.swift, which is self.target = target. I set a breakpoint right before that line and executed e target in the debugger; here's what I got:
(lldb) e target
(AnyObject?) $R1 = (instance_type = 0x0000608000164c80) {
instance_type = 0x0000608000164c80 {
AppKit.NSTextView = {
baseNSText#0 = <extracting data from value failed>
}
title = "some random text"
minimumWidth = 100
}
}

I found several answers.
How you search Google remains important. I varied my searches some more and came upon this here on SO, the short version of which is that it says you can't form a weak reference specifically to NSTextView and includes a link to explanatory Clang documentation.
Perhaps more interestingly, I also found the answer for "erratic" errors I mentioned in the title - one of the machines I develop on turns out to have Swift 3.1, but the other has 3.0.2. The more recent version does not exhibit the error forming the weak link, suggesting Apple has upgraded the NSTextView implementation.

Related

Text label not updating on screen, even though it shows up on `print()`

I'm writing an application which has an NSSplitViewController as the main View-Controller. I have it linked-up so that clicking a button in the menubar will trigger an #IBAction which then calls a function in one of the sub-View-Controllers.
if let board = storyboard {
let imageController = board.instantiateController(withIdentifier: "VC_image_ID") as! VC_image
imageController.viewDidAppear() // necessary or else I'll get an "Unexpectedly found nil" later on
DispatchQueue.global().async{imageController.processImage(path)} // path variable was set earlier in the code, but not shown here
}
Inside the sub-View-Controller (named VC_image), I'm trying to change the stringValue of a label by using the following code:
public func processImage(_ path: String) {
DispatchQueue.main.async {
self.imageText.stringValue = path
print(self.imageText.stringValue)
}
}
Although the imageText.stringValue actually seems to have changed based on the fact that it prints the updated text (through the console), the text in the window never changes. Why is this happening? The reason is probably really obvious to professionals, but I'm still an amateur and can't figure it out. Thanks.

Type has no member

I'm new to swift and I'm trying to create a very simple app but for some reason, the variables are not working.
I'm not really sure what the problem is but I've tried changing var to let.
class ImageViewController: ViewController {
var blocks = [Block(size: 100, centerX: 100, centerY: 100, code: "1", color: image.colorDic["1"]!)]
var codes = ["1"]
var colors = [UIColor(named: "Red")]
//create image
var image = Image(blocksArray: blocks, code: codes, color: colors)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .blue
title = "Title"
for block in image.blocksArray{
view.addSubview(block.block)
}
// Do any additional setup after loading the view.
}
On the line where I create the image, I get an error that says
"Type 'ImageViewController' has no member 'blocks'"
Also, right under the line where I create 'colors' I'm also getting
"Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee74cbf48)"
your property image access the block property before they are both initilized, to fix your problem your variable "image" should be flagged as lazy variable, meaning it will be initilized after the other properties have been initilized hence it is safe to access those other properties.
More about lazy properties
The official Swift documentation says,
"Lazy properties are useful when the initial value for a property is dependent on outside factors whose values are not known until after an instance’s initialization is complete."
You can read more about lazy properties here: - Swift Properties Official documentation
Here is how to do it: -
lazy var image: Image = {
return Image(blocksArray: blocks, code: codes, color: colors)
}()
for the case of colors part, I think it is a good practice to avoid typing strings when initializing especially for simple colors like yours and use the enum versions.
Change it to this : -
var colors = [UIColor.red]
or this: -
var colors: [UIColor] = [.red]

Making NSView appearance vibrantDark

I'm currently learning Swift, and I followed this tutorial to create an NSWindow that inherits the vibrantDark property from the AppKit in Swift. The code I added to the WindowController.swift file is as follows:
window?.titleVisibility = .hidden
window?.appearance = NSAppearance(named: NSAppearance.Name.vibrantDark)
What I'd like to do is achieve the same result for an NSPopover in my program; however, when I add the following to my LogViewController.swift file, I get an error -- the first of which is "Value of type 'NSView' has no member 'titleVisibility,'" and the second of which is "Cannot use optional chaining on non-optional value of type 'NSView.'"
view?.titleVisibility = .hidden
view?.appearance = NSAppearance(named: NSAppearance.Name.vibrantDark)
Several posts have addressed this issue for NSWindow, but I cannot find an answer that addresses NSPopover. I currently have the following condition set to open NSPopover upon clicking NSImage in the status bar:
popover.contentViewController = LogViewController.freshController()
I'm thinking that having NSPopover as NSView is what's causing the issue, but -- being as I'm still new to Swift -- I'm not sure how to diagnose this the problem. That said, I would much appreciate it if anyone could point me towards the right direction.
You should be using an NSPopover, not an NSView:
var myPopover: NSPopover?
myPopover = NSPopover.init()
myPopover?.appearance = NSAppearance(named: .vibrantDark)
If you want to use a contentViewController:
var popoverViewController: NSViewController?
myPopover?.contentViewController = self.popoverViewController

Swift 2: Selector in UIPinchGestureRecognizer: How to access func from another class

I'm following this class on Swift and building apps.
At 43:30 in the video, the instructor teaches how to set up a UIPinchGestureRecognizer, which takes in a function from another file in its Selector.
This is the code the instructor uses:
#IBOutlet weak var faceView: FaceView! {
didSet {
faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: #selector(FaceView.changeScale(_:))))
updateUI()
}
}
I get 2 errors:
Expected expression in list of expressions,
and:
Expected ',' separator.
I have tried changing #selector to Selector with no luck.
the function changeScale:
func changeScale(recognizer: UIPinchGestureRecognizer)
{
switch recognizer.state {
case .Changed, .Ended:
scale *= recognizer.scale //scale is the displayed image scale
recognizer.scale = 1.0
default:
break
}
If I surround the Selector argument with quotes, the app crashes when I pinch, giving the following error:
unrecognized selector sent to instance.
I am running Xcode 8.1(8B62) and a iPhone 5 simulator on MacBook Air (Ver 100.11.6)
This is the code I used that works
faceView.addGestureRecognizer(UIPinchGestureRecognizer(target:faceView,
action:#selector(FaceView.changeScale(recognizer:))))
When you pinch the face, make sure click "Option + Left Click + Movement on your mousepad". I make the mistake of just pressing "Option + Movement on the mousepad" and it does not work.
Hope it helps.
As can be seen in the comments above, the Xcode version is 7.2 and the #selector syntax was introduced in Xcode 7.3 and therefore not available here. This just means that you should be able to use the "old" Selector syntax.
The difference is that you just give pass a strings to the Selector with the name of your function and then a : for each of the parameters your function requires. You require one parameter (the recognizer) so in your case the string looks like this:
"changeScale:"
So you'd use:
Selector("changeScale:")
And you end up with:
#IBOutlet weak var faceView: FaceView! {
didSet {
faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: Selector("changeScale:")))
}
}
As you can see, this is error prone...one typo and kaboom! Which is why the new #selector syntax is a fine improvement...sorry...not trying to rub it in.
Hope this helps you.

Swift 2 unrecognized selector in tapgesture

I recently started to update my app to Swift 2.0, but I've run into a problem that has a hundred different answers on SO, none of which seemed to be related to my problem.
(This worked before updating the application to Swift 2.0), but I havn't been able to find any changes made to tap gesture recognisers?
Here's the full error I'm getting:
[Stocks.SidePanelViewController proFormulaTapGesture]: unrecognized selector sent to instance 0x14f06ae00
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Stocks.SidePanelViewController proFormulaTapGesture]: unrecognized selector sent to instance 0x14f06ae00'
*** First throw call stack:
(0x1830904d0 0x197afff9c 0x1830971ec 0x183094188 0x182f9920c 0x188c3c58c 0x188899b50 0x18872d104 0x188c3bc30 0x1886ed28c 0x1886eb3ac 0x18872b088 0x18872a5ec 0x1886fb908 0x1886f9fac 0x183047d6c 0x183047800 0x183045500 0x182f75280 0x18e0ec0cc 0x188762df8 0x100158184 0x1983428b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
It's a simple tap gesture. But it seems to not recognize the selector anymore.
Here's the code I use to set up the recognizer:
let proFormulaTap = UITapGestureRecognizer()
proFormulaTap.addTarget(self, action:"proFormulaTapGesture")
proFormulaView.addGestureRecognizer(proFormulaTap)
Here's the function I'm trying to run:
func proFormulaTapGesture() throws {
print("proFormulaTapGesture")
selectView(proFormulaView)
selectedMenuItem = 0
Formula = 1
menuTabBarController.selectedIndex = 0
navigationController?.navigationBar.topItem!.title = "BASIC FORMULA"
try (menuTabBarController.viewControllers as! [SuggestionsViewController])[0].loadSuggestions()
}
However since it never prints "proFormulaTapGesture" in the console. I definitely think the error occurs before the function. Also suggested by the error mentioning the selector.
Obviously try/catch has been added to the function since the update to Swift 2.0, but nothing has been changed in the setup of the tapGestureRecognizer.
I tried removing the "throws" and try from the function, but the problem perists. I also tried making a button instead of a tap gesture recognizer. But I'm still getting the same error, suggesting it might be a problem with the selector (function) and not the tap gesture / button. However all my other buttons in my app works just fine?
I also tried to rename the selector/function, and the tap gesture recognizer. Still the same.
The original Code was programmed in Swift, not Obj-C. The throws and try, was added during Apple's code conversion to Swift 2.0
Any help to why this is suddenly broken would be greatly appreciated.
Thanks!
The issue may be that UIKit does not know how to call a selector that throws, at least in this context.
Remember that UIKit will be calling this function in response to a tap action. It is not accustomed to calling a function that throws; obviously the throw must alter the method's call signature as far as the Objective-C runtime is concerned. Beyond that, if your function proFormulaTapGesture did throw an error, what would UIKit do with the error even if it could catch it?
I'm not sure where you added do/try/catch. But unless it was inside your action function proFormulaTapGesture, I do not see how it could be relevant. I believe that you will need to implement the try/catch error handling inside proFormulaTapGesture, so that it effectively swallows the error, so that proFormulaTapGesture does not throw.
I just created a quick test project myself and found that a throwing target action gives the same "Unrecognized selector" error you got:
override func viewDidLoad() {
super.viewDidLoad()
self.tapper = NSClickGestureRecognizer(target: self, action: "didClick")
self.view.addGestureRecognizer(self.tapper!)
}
func didClick() throws {
print("did click")
}
2015-07-27 00:16:43.260 Test1[71047:54227175] -[Test1.ViewController didClick]: unrecognized selector sent to instance 0x6000000c5010
...
I believe you will need to rework your code to handle the error internally in your action function so that it does not throw - something like this:
func proFormulaTapGesture() {
print("proFormulaTapGesture")
selectView(proFormulaView)
selectedMenuItem = 0
Formula = 1
menuTabBarController.selectedIndex = 0
navigationController?.navigationBar.topItem!.title = "BASIC FORMULA"
do {
try (menuTabBarController.viewControllers as! [SuggestionsViewController])[0].loadSuggestions()
} catch {
// Handle the error here somehow
}
}
If you are unable to handle the error within proFormulaTapGesture, you'll have to be more creative with passing it out of the function (think NSNotification, etc...).
To be clear, I created a new Single View iOS application in Xcode 7 beta 4, and used this exact code as my ViewController. No other changes. The app compiles and works just fine in the iOS simulator.
#robertvojta also points out that if didTap below is marked private, #objc is also required to avoid a dynamic dispatch crash.
import UIKit
class ViewController: UIViewController {
var tapper: UITapGestureRecognizer?
override func viewDidLoad() {
super.viewDidLoad()
self.tapper = UITapGestureRecognizer(target: self, action: "didTap")
self.view.addGestureRecognizer(self.tapper!)
}
func didTap() {
print("did Tap")
}
}
(Note I also tested on an OS X project, which is the code I used earlier in the answer).