Fixed compiler issues. Starting to get very annoyed about optionals.

This commit is contained in:
Pietro Gagliardi 2015-08-07 19:29:01 -04:00
parent 4857ddc018
commit 96e857dbee
2 changed files with 20 additions and 10 deletions

View File

@ -4,11 +4,18 @@ import Cocoa
var spaced = false var spaced = false
var firstvert = true var firstvert = true
// keep alive
// apparently I'm not allowed to declare a variable and then assign to it first thing in a function
// it'd be great if people weren't so afraid of nil pointers
var keepAliveMainwin: Window? = nil
func appLaunched() { func appLaunched() {
var mainwin = Window() var mainwin = Window()
mainwin.SetMargined(spaced) mainwin.SetMargined(spaced)
mainwin.Show() mainwin.Show()
keepAliveMainwin = mainwin
} }
class appDelegate : NSObject, NSApplicationDelegate { class appDelegate : NSObject, NSApplicationDelegate {

View File

@ -9,29 +9,34 @@ func isAmbiguous(view: NSView, indent: Int) {
view.window?.visualizeConstraints(view.superview!.constraints) view.window?.visualizeConstraints(view.superview!.constraints)
} }
for subview in view.subviews { for subview in view.subviews {
tIsAmbiguous(subview as! NSView, indent + 1) isAmbiguous(subview as! NSView, indent + 1)
} }
} }
class Window : NSWindow, Control { class Window : NSWindow, Control {
private var c: tControl? private var c: Control?
private var margined: Bool private var margined: Bool
init() { init() {
self.c = nil
self.margined = false
// we have to initialize our own instance variables first, unfortunately (thanks erica in irc.freenode.net/#swift-lang)
super.init( super.init(
contentRect: NSMakeRect(0, 0, 320, 240), contentRect: NSMakeRect(0, 0, 320, 240),
styleMask: (NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask), styleMask: (NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask),
backing: NSBackingStoreType.Buffered, backing: NSBackingStoreType.Buffered,
defer: true) defer: true)
self.title = "Auto Layout Test" self.title = "Auto Layout Test"
self.c = nil }
self.margined = false
required init?(coder: NSCoder) {
fatalError("can't use this constructor, sorry")
} }
func SetControl(c: Control) { func SetControl(c: Control) {
self.c = c self.c = c
var contentView = self.contentView as! NSView var contentView = self.contentView as! NSView
contentView.addSubview(self.c?.View()) contentView.addSubview(self.c!.View())
self.Relayout() self.Relayout()
} }
@ -43,7 +48,7 @@ class Window : NSWindow, Control {
func Show() { func Show() {
self.cascadeTopLeftFromPoint(NSMakePoint(20, 20)) self.cascadeTopLeftFromPoint(NSMakePoint(20, 20))
self.makeKeyAndOrderFront(self) self.makeKeyAndOrderFront(self)
tIsAmbiguous(self.contentView as! NSView, 0) isAmbiguous(self.contentView as! NSView, 0)
} }
func View() -> NSView { func View() -> NSView {
@ -59,13 +64,11 @@ class Window : NSWindow, Control {
return return
} }
var contentView = self.w.contentView as! NSView var contentView = self.contentView as! NSView
contentView.removeConstraints(contentView.constraints) contentView.removeConstraints(contentView.constraints)
// TODO why can't I just say var views = [ "view": p.view ]?
// I think the parser is getting confused
var views = [ var views = [
"view": self.c?.View(), "view": self.c!.View(),
] ]
var margin = "" var margin = ""
if self.margined { if self.margined {