• Welcome to the new COTI server. We've moved the Citizens to a new server. Please let us know in the COTI Website issue forum if you find any problems.

Adventures in Xcode 6.4...

aramis

Administrator
Administrator
Baronet
Preface: I've been coding a long time. about 35 years. I've seldom done anything terribly useful. (There are a few useful bits I've done in Javascript + HTML 4, and in C/C++ for Palm, and some useful stuff in QuickBasic back in the 90's. Ugly, but it worked.)

So... i've been trying to make the leap to OOP... and not been finding it terribly happy.

So I figure I'll share my experience with ^&%&%^%^&**%^&^& Xcode...

Prior to this week, all my use of Xcode was simply to compile useful bits for brute forcing statistical calculations. Compile to command line.

So, I see a new option for language for command line apps- Swift. Unlike C/C++/ObjC, the template file doesn't include how to retrieve argv*/argc. I also see the "playground."

The playground is a JITC implementation of swift. Great for grasping basic language syntax. Stupidly fast, too. And you can set a run-time limit, too.

So, I set up a playground, and figure out the loop controls.
So far, so swift. About an hour, and I'm feeling confident in the basic syntax and how to access variables and arrays - rather python-like. Slicing can be done - not as easily as python, but the tools are there (as methods on arrays).

Next I try for a hello world. I can make one in console. Cool. all of 10 minutes more.

Next, I try for one that uses OOP and the layout tools. 24 hours of mixed searching and code failures later, I finally grok what's wrong.

See, the cocoa app templates creates a bunch of files.
The big 3 are: AppDelegate.swift, ViewController.swift, main.storyboard.

AppDelegate is the initialization code.
ViewController is the routines for events triggered by interface elements
the storyboard is equivalent to the old nib file... where you create your visual UI.

The default setup does compile, and does create a blank window.

What I couldn't get to happen was the controls to actually implement. I couldn't get anything to show up...

The issue is that you have to drag interface items into the actual code file to get them linked up, THEN put the function code inside the resulting function.

Simple? deadly so. As in, so simple, and so out of the box, that it never occurred to my spaghetti coder brain that it was THAT simple - the storyboard drag-and-drop. It doesn't help, tho', that the default view is a single file, but you MUST have two windows open to get it to work.

So, the best way seems to be:
view ⇒ assistant editor ⇒ on right
view ⇒ assistant editor ⇒ show assistant editor

This gives a 4 column view
File List
...
...
standard editor
...
...
Assistant editor
...
...
inspector
...
parts list

You put the main.storyboard in the standard editor by clicking in the file list.
You add views and/or interface elements by dragging them from the parts list. As you do, the ViewController.swift will automatically appear in the assistant editor.
You then need to line up the assistant editor to show the area you want to drop that interface element's definition into... and then drag the control off the storyboard into the code with a control-click (or right click), and let go where you want it. It generates the (visible) code snippet for either a defined outlet or a defined function event. (You pick which by a popup).

Then, for functions, you add your code to it.

Now, I've done OOP before - events trigger your code chunks. It's cool. But it never occurred to me to drag the visual element into the code to link to it.

So long as your controls are single function, all is good.

If you want to change the text in a control, you need to define it as an outlet.
If you want a function from a control, you need to define it as a function.
The popup lets you select outlet or function, name, type of action, and memory type. (WTF? stong or weak memory?)

changing button text is just pushing strings.
drag it to make it an outlet.
the .title is what it shows when on. it's a string
if you can figure out attributed strings, .attributedstring allows formatting it

The documentation is not good for novices (and to be blunt, I've never made it past novice in the coding department...)

Swift does allow the full unicode character set.

More later, as I explore.
 
This all sounds vaguely familiar. I took an iOS class a few years ago, and we learned XCode, and yeah, the big disconnect was in knowing how to connect UI widgety thingies to their data providers, or whatever.

I have yet to actually sit down and start learning Swift, but I want to. There are apps to write. In my "abundant free time".
 
Yes, quite the learning curve. I'm reading the Swift 2.0 pre-release iBook (free), documents on the Apple Developer Connection site, and watching videos.

I ran across a sale offer on a $300 35 hour video tutorial called iOS 9 Swift Coding Course for $59 and am wading through that. The nice thing is that it is up-to-date: Swift 2.0 and the current Xcode Beta. Very helpful.
 
You can create the object functions and outlet variables then link them... when you create them yourself, a small circle appears to their left. hover over that circle until it darkens, then drag to the control in the storyboard.

Still, you have to drag. What a drag.
 
You can create the object functions and outlet variables then link them... when you create them yourself, a small circle appears to their left. hover over that circle until it darkens, then drag to the control in the storyboard.

Still, you have to drag. What a drag.

Are you sure about that? If you hover over the small circle and the control highlights in the view controller then it should already be linked with the drag being unnecessary.
 
Are you sure about that? If you hover over the small circle and the control highlights in the view controller then it should already be linked with the drag being unnecessary.

Yes. If you manually write the @IBoutput or @IBcontrol line, you have to drag from the hollow circle (which illuminates nothing) to the control on the storyboard you want it to link to.

It saves a bunch of typing doing it backwards... just as many drags but not so much typing, especially for a group of related controls with similar names desired. Copy into text editor, edit, paste new batch in, save, then drag the empty circles over to the controls.
 
I fired up XCode right before heading to work this morning. I've got the sandbox mode on, but it sounds like I should really just create an application and dive in.
 
Bulk of the iOS devs I know build their UIs by hand, and don't use the tools in Xcode. They avoid the GUI builder, story boards, auto layout, the whole kit. More of a pain than it's worth to them.
 
It depends on the app you're creating, and, perhaps, what sort of level of effort you've got going in-house. I found the built-in widgets fine, but (for example) if you're writing a game, you're already not going to use those.
 
It depends on the app you're creating, and, perhaps, what sort of level of effort you've got going in-house. I found the built-in widgets fine, but (for example) if you're writing a game, you're already not going to use those.

Yea I'm not talking about the widgets, just the overall layout of the assorted views, their hierarchies and relationships, and such.
 
Thanks to Wil / Aramis for starting this out and getting me back into iOS.

This morning I created a "tabbed" application -- one that has multiple views accessed by a toolbar on the bottom.

I had to learn the 'for' statement in Swift (egads it's like I'm a child all over again). I punted and stayed with the most C-like version:

Code:
for var a = 1; a < 7; a++ 
{
   // stuff here
}
Then I learned that Swift really wants me to be careful, and so it requires braces around the code block of an "if" clause, even if there's only one statement:

Code:
if ( a < 6 )
{
   upp += ", ";
}
The semicolon was optional. I do it due to my own personal insecurities.

So after 25 minutes, I can roll UPPs (see attached image). Modest beginnings.
YAAAAAElFTkSuQmCC
 

Attachments

  • my ios app.png
    my ios app.png
    5.9 KB · Views: 4
After a foray into the backyard to do some gardening...

I flounder around with pickers. Back in the Objective-C days, we did things with @property, and created a Header file (.h) as well as a main/module/code file (.m).

Nowadays? I don't know. Does Swift use header files?

I've got my picker. Now I'm trying to create a class which implements the data source interface for it. Because I don't want to throw piles of junk into my view controller.

So I'll go surfing for the answer.

After lunch.
 
OK, here's how to do pickers the Proper Way:

1. Get your view controller on half of the screen, and the storyboard on the other half, as Aramis/Wil instructed.

2. Drag a Picker View onto your storyboard.

3. Control-click-drag that picker into your view controller, and create an @IBOutlet there.

4. Now create a new class -- I call mine CareerDataSource -- to manage the picker's data. Add 'import UIKit' below the first import line. Next, I found it had to explicitly extend NSObject, which seems weird. I added the UIPickerViewDataSource and UIPickerViewDelegate protocols (interfaces) to the class line.

The new class has the actual selection choices in a very simple array:

Code:
let pickerData = [ "Merchant", "Scout", "Marine", "Agent", "Soldier" ]

Then, per the protocols, I implemented three methods:

Code:
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count
    }
    
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return pickerData[row]
    }

5. Back to the view controller. I instantiate this class:

Code:
    var careerDataSource = CareerDataSource.new()

Then, in viewDidLoad(), I point the picker to my new class:

Code:
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        careerPicker.delegate = careerDataSource
        careerPicker.dataSource = careerDataSource
    }


Waa lah!
 

Attachments

  • chargen 2.png
    chargen 2.png
    10.9 KB · Views: 2
Back
Top