Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#368848 - 16/05/2017 05:29 Xcode/Swift folks? Any experts here?
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31596
Loc: Seattle, WA
anyone here good with swift/Xcode?

I'm trying to write a Mac desktop version of a utility program that I had previously written on Windows. I can't get past the Hello World stage.

Basically I put some text and button controls on the ViewController, but I can't write to them despite them having working outlet connectors. Basically I just want to write some text to the text control, but can't, because the value of the outlet connectors is always nil. So for instance, when I try to programmatically write text to the text control, or programmatically change the text on the face of the button, it gets an "Invalid instruction" crash because it's Nil.

But these things work though. For instance I can put code in the button's action and I can make it display a dialog when I click on the button. So the button is there, it's instantiated, it works. But try to do anything *to* the button and it's crash city.

I've googled like crazy and tried a shitload of different things but most of the things people suggest are for older versions of XCode and literally don't even compile now.

Any Xcode/Swift experts here wanna try my project file and look to tell me what I did wrong?
_________________________
Tony Fabris

Top
#368849 - 17/05/2017 05:59 Re: Xcode/Swift folks? Any experts here? [Re: tfabris]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31596
Loc: Seattle, WA
My issue appears to be this one:
http://stackoverflow.com/questions/35104...kes-objects-nil

But the code example given there to fix it refuses to compile, stating that "UIStoryboard" is an undeclared type.
_________________________
Tony Fabris

Top
#368855 - 18/05/2017 07:04 Re: Xcode/Swift folks? Any experts here? [Re: tfabris]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31596
Loc: Seattle, WA
For posterity:

I solved my own problem in an unconventional way. I didn't find this method anywhere online, but it works for me.

I need to be able to reference the ViewController object (the thing that handles the user interface and the controls I put on the user interface) after it's already been automatically created at program startup. Supposedly this is the "right" way to do it, by accessing the main storyboard by its named identifier, but that wouldn't compile in my project, with various parts of that example code refusing to even compile. Keywords like "UIStoryboard" and "instantiateViewControllerWithIdentifier" are simply not recognized by the Xcode compiler at all, saying that they are undeclared identifiers. I don't know why this is the case because every example on the web says "this is the way to do it". I can only imagine that these examples are all from old versions of Swift/Xcode and there is some kind of a new paradigm that I'm not finding in my Google searches.

Since for this particular application, I don't need to be creating and deleting windows, views or storyboards (it's mainly got a single UI window that never changes except for some text output to a logging window), I was able to turn the ViewController into a weird sort of a Singleton and reference it that way. Though there are examples of singletons on the web, I haven't seen an example of making a ViewController singleton, but after some poking at it, I suceeded in doing it. I did it like this:

In the ViewController, at the class level, create a variable named "sharedInstance". Examples of doing something like this for singletons exists on the web already, but I had to do it a special way, starting off with the variable set to nil, and also using the Optional indicator (the question mark), because it wouldn't compile unless I put it there.
Code:
    static var sharedInstance: ViewController? = nil


Then, inside the "viewDidLoad()" function that was already there in the ViewController, I set the variable for sharedInstance to "self". I had to do this after the view loaded so that it references an actual instance of the currently-running already-initialized ViewController.
Code:
    override public func viewDidLoad()
    {
        super.viewDidLoad()

        ViewController.sharedInstance = self
    }


Now I am finally able to reference the ViewController and its control outlets from any module in the program by referring to it the same way you'd refer to any singleton, with the added trick that I also needed to put the Optional indicator (the question mark) here too:
Code:
            // Log some text to the Text Field control on the main storyboard:
            ViewController.sharedInstance?.TextField.textStorage?.append(NSAttributedString(string: "Hello World!\r\n"))
_________________________
Tony Fabris

Top