windowNibName error in Swift / Cocoa?

Well, that will teach me to set down Swift programming for a year and a half. Apparently things have changed.In my past programs, this inside my MainWindowController was quite happy:class MainWindowController: NSWindowController { override var windowNibName: String? { return "MainWindowController" }...But now I'm getting this error: Property 'windowNibName' with type 'String?' cannot override a property with type 'NSNib.Name?'I've been Googling and poking around Stack Overflow but have been unable to figure out what the exact pro...Read more

@selector() in Swift?

I'm trying to create an NSTimer in Swift but I'm having some trouble.NSTimer(timeInterval: 1, target: self, selector: test(), userInfo: nil, repeats: true)test() is a function in the same class.I get an error in the editor: Could not find an overload for 'init' that accepts the supplied argumentsWhen I change selector: test() to selector: nil the error disappears.I've tried:selector: test()selector: testselector: Selector(test())But nothing works and I can't find a solution in the references....Read more

#pragma mark in Swift?

In Objective C, I can use #pragma mark to mark sections of my code in the symbol navigator. Since this is a C preprocessor command, it's not available in Swift. Is there a stand-in for this in Swift, or do I have to use ugly comments?...Read more

Swift Beta performance: sorting arrays

I was implementing an algorithm in Swift Beta and noticed that the performance was very poor. After digging deeper I realized that one of the bottlenecks was something as simple as sorting arrays. The relevant part is here:let n = 1000000var x = [Int](repeating: 0, count: n)for i in 0..<n { x[i] = random()}// start clock herelet y = sort(x)// stop clock hereIn C++, a similar operation takes 0.06s on my computer.In Python, it takes 0.6s (no tricks, just y = sorted(x) for a list of integers).In Swift it takes 6s if I compile it with the fo...Read more

swift - How to tell if NSWindow is being used to browse document versions (right side of version browser UI)

The OS X document versions browser (get to it from File > Revert to > Browse All Versions) shows the current version on the left side and and stack of older versions on the right side.The window controller on the left side gets notifications letting you know that it's entering/exiting version browsing mode. Also the document for the left hand side returns true. in response to isBrowsingVersions.But I don't know how to tell when my window is being used to display one of the actual version (the stack on the left side). Those windows don't get the...Read more

swift - Appsync return 401 errors when connecting with cognito

So I've set up cognito and appsync and connected them both to my iOS client. Appsync works great from the console, but when i make any requests from iOS i get a 401 error without any error messages. I'm able to sign in and out of cognito fine. I think i might be passing the wrong thing in to something maybe?Here's my app delegate code: import UIKit import AWSAppSync import AWSS3 import AWSCognitoIdentityProvidervar credentialsProvider: AWSCognitoCredentialsProvider?var pool: AWSCognitoIdentityUserPool?@UIApplicationMainclass A...Read more

Swift 4 / Xcode 9.3 / iOS - Catch The file "x" couldn’t be opened because there is no such file with String(contentsOf: url)

Swift 4 / Xcode 9.3 / iOSI'm using String(contentsOf: url) and in some cases I get an error because the file referenced by the URL doesn't exist. I want to handle that specific error case.I know I can check to see if the file exists first, but I want to handle the error case instead.Right now I am doing:let data = try String(contentsOf: url)Up until now I've been catching all errors, with something like:do { let data = try String(contentsOf: url) //<other code here>} catch let error { fatalError("bad error: \(error)")}When the s...Read more

swift - Is it possible to open a screen directly in XCUITest?

I have 3 screens, lets say,LoginForgot PasswordHelp screenBy default the Login screen open when the app starts. The Forgot Password screen is shown when you click on the Forgot Password button, and Help Screen opens when the Help link is clicked.Can I somehow open the the Forgot Password Screen directly without going through the procedure of clicking the button using XCUITest?I am suggesting something in the same lines as passing an adb intent to open a View directly....Read more

swift - GLKit vs. Metal perspective matrix difference

I'm reading a Metal tutorial on raywenderlich.com, where it introduces a pure Swift float4x4 helper class. 99% it's just wrapper around GLKit functions, except one function which really puzzles me: static func makePerspectiveViewAngle(_ fovyRadians: Float, aspectRatio: Float, nearZ: Float, farZ: Float) -> float4x4 { var q = unsafeBitCast(GLKMatrix4MakePerspective(fovyRadians, aspectRatio, nearZ, farZ), to: float4x4.self) let zs = farZ / (nearZ - farZ) q[2][2] = zs q[3][2] = zs * nearZ return q }Why does it need to change q[2...Read more

Swift casting Any to closure

I'm trying to cast a function stocked as Any in a Dictionary.The cast fails, and I don't really understand why. I tried to search for similar examples with no luck.fileprivate let array: [[String:Any]] = [ [ "title": "my title", "image": "image url", "callback": callback ]]func callback() { print("called")}if let callback = array[0]["callback"] as? () -> Void { callback()}Any help is welcome!Cheers...Read more

swift - Referencing self in super.init

I have the following code (EDIT: Updated the code so everyone can compile it and see):import UIKitstruct Action{ let text: String let handler: (() -> Void)?}class AlertView : UIView{ init(actions: [Action]) { super.init(frame: .zero) for action in actions {// let actionButton = ActionButton(type: .custom)// actionButton.title = action.title// actionButton.handler = action.handler// addSubview(actionButton) } } required init?(coder aDecoder: NSCoder) { fata...Read more

Swift: Not initialised Lazy Variable that returns Closure

I have a lazy variable that is not initialised:lazy var time: () -> String = { return String(describing: Date())}Whenever I call time() I get a new Date. So it seems like it behaves exactly like: var time: () -> String { return { return String(describing: Date()) }}In this context, is it bad practice just to use the lazy closure, because then I don't have to write two return-statements in a row or do I miss something?...Read more

Using swift Generic Types with protocols

I've tried to use generic type with protocol:class Weak<T: AnyObject> { weak var value: AnyObject? init (value: AnyObject) { self.value = value }}protocol SomeProtocol: AnyObject { func doSomething()}func createWeak(object: SomeProtocol) -> Weak<SomeProtocol> { return Weak<SomeProtocol>(value: object)}class SomeClass: SomeProtocol { func doSomething() { print("Some 2") }}let temp = SomeClass()let weakObject = createWeak(object: temp)weakObject.value?.doSomething()And got the compiler erro...Read more