All Questions
8,573
questions
629
votes
19
answers
111k
views
Do Swift-based applications work on OS X 10.9/iOS 7 and lower?
Will Swift-based applications work on OS X 10.9 (Mavericks)/iOS 7 and lower?
For example, I have a machine running OS X 10.8 (Mountain Lion), and I am wondering if an application I write in Swift will ...
319
votes
21
answers
162k
views
Getting a "This application is modifying the autolayout engine from a background thread" error?
Been encountering this error a lot in my OS X using swift:
"This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. ...
284
votes
19
answers
237k
views
Check OS version in Swift?
I'm trying to check system information in Swift. I figured out, that it could be achieved by code:
var sysData:CMutablePointer<utsname> = nil
let retVal:CInt = uname(sysData)
I have two ...
262
votes
20
answers
162k
views
Getting the difference between two Dates (months/days/hours/minutes/seconds) in Swift
I am trying to get the difference between the current date as NSDate() and a date from a PHP time(); call for example: NSDate(timeIntervalSinceReferenceDate: 1417147270). How do I go about getting the ...
224
votes
22
answers
204k
views
How to remove all subviews of a view in Swift?
I'm looking for a simple method to remove at once all subviews from a superview instead of removing them one by one.
//I'm trying something like this, but is not working
let theSubviews : Array = ...
219
votes
13
answers
173k
views
NSRange from Swift Range?
Problem: NSAttributedString takes an NSRange while I'm using a Swift String that uses Range
let text = "Long paragraph saying something goes here!"
let textRange = text.startIndex..<text.endIndex
...
162
votes
9
answers
86k
views
How to use Namespaces in Swift?
The documentation only mentions nested types, but it's not clear if they can be used as namespaces. I haven't found any explicit mentioning of namespaces.
157
votes
6
answers
93k
views
Fatal error: use of unimplemented initializer 'init(coder:)' for class
I decided to continue my remaining project with Swift. When I add the custom class (subclass of UIViewcontroller) to my storyboard view controller and load the project, the app crashes suddenly with ...
155
votes
7
answers
147k
views
How to get a user's time zone?
Does anyone know of a way to get a users time zone in Swift?
I'm getting a specific time something is on t.v. out of a database and then need to subtract/add from where they are located to show them ...
150
votes
6
answers
62k
views
How do you access command line arguments in Swift?
How do you access command line arguments for a command line application in Swift?
141
votes
16
answers
115k
views
Bundle.main.path(forResource:ofType:inDirectory:) returns nil
Try not to laugh or cry -- I'm just getting back into coding after 20 years out...
I've spent more than 4 hours looking at references and trying code snippets to get Bundle.main.path to open my text ...
117
votes
10
answers
78k
views
How can I convert from degrees to radians?
I am trying to convert this Obj-C code to Swift code but I don't know what the equivalent of this code should be ?
#define DEGREES_TO_RADIANS(degrees)((M_PI * degrees)/180)
I googled and found this
...
116
votes
9
answers
74k
views
Swift - which types to use? NSString or String
With the introduction of Swift I've been trying to get my head round the new language
I'm an iOS developer and would use types such as NSString, NSInteger, NSDictionary in an application. I've ...
112
votes
2
answers
3k
views
Problem setting video frame rate using AVAssetWriter/AVAssetReader
Situation:
I am trying to export video with some parameters like video bit rate, audio bit rate, frame rate, changing video resolution, etc. Note that I am letting the user set the video frame rate in ...
102
votes
3
answers
29k
views
xcrun: error: unable to find utility "xctest", not a developer tool or in PATH
I am using Xcode command line tools instead of XCode app in my MAC.
When i use swift --version
I get Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)
I cloned a project with
git ...
102
votes
20
answers
130k
views
Input from the keyboard in command line application
I am attempting to get the keyboard input for a command line app for the new Apple programming language Swift.
I've scanned the docs to no avail.
import Foundation
println("What is your name?")
???
...
89
votes
8
answers
69k
views
OSX Swift open URL in default browser
How to open a URL in the system default browser by using Swift as programming language and OSX as platform?
I found a lot with UIApplication like:
UIApplication.sharedApplication().openURL(NSURL(...
89
votes
12
answers
57k
views
Read a file/URL line-by-line in Swift
I am trying to read a file given in an NSURL and load it into an array, with items separated by a newline character \n.
Here is the way I've done it so far:
var possList: NSString? = NSString....
87
votes
5
answers
10k
views
unable to print object ('po') in xcode6 beta 6 osx swift project: (Error in auto-import: failed to get module '__ObjC' from AST context)
An attempt to print object (po command) in xcode 6 beta 6 OSX Swift project results in this error message:
(lldb) po managedObject
error: Error in auto-import:
failed to get module '__ObjC' from AST ...
75
votes
1
answer
17k
views
Xcode Project Format: what are the differences between 3.1, 3.2, 6.3 and 8.0?
When I open a project in Xcode 9 (or previous versions) and check the File Inspector, there's a section called Project Document, with a combo box named Project Format.
The available options (in Xcode ...
73
votes
7
answers
52k
views
How to determine device type from Swift? (OS X or iOS)
I know Swift is relatively new, but I was wondering if there was a way to determine the device type?
(Like you used to be able to do with a #define)?
Mainly I would like to know how to differentiate ...
63
votes
9
answers
19k
views
How can I output to STDERR with Swift?
I'm trying to make a command line tool for OS X with Xcode 6 and the new Swift language. How can I send output to stderr? Is this done with println?
60
votes
2
answers
15k
views
Why Swift is 100 times slower than C in this image processing test? [duplicate]
Like many other developers I have been very excited about the new Swift language from Apple. Apple has claimed its speed is faster than Objective C and can be used to write operating system. And from ...
58
votes
4
answers
16k
views
Can swift closures be set to a default value when used as a parameter in a function?
A pretty handy feature of Swift functions is that function parameters can have default values:
func someFunction(parameterWithDefault: Int = 42) {
//if no arguments are passed to the function ...
57
votes
5
answers
50k
views
Is there a swift native function to convert a number to a hex string?
Is there any native Swift way for any (at least integer) number to get its hexadecimal representation in a string? And the inverse. It must not use Foundation. For example the String class has a ...
55
votes
7
answers
17k
views
How to listen to global hotkeys with Swift in a macOS app?
I'm trying to have a handler in my Mac OS X app written in Swift for a global (system-wide) hotkey combo but I just cannot find proper documentation for it. I've read that I'd have to mess around in ...
52
votes
3
answers
22k
views
If not let - in Swift
is there is a way to negate the "if let" in swift?
This looks silly to me:
if let type = json.type {
} else {
XCTFail("There is no type in the root element")
}
I can't use ...
46
votes
3
answers
36k
views
Can't initialize a struct with a default value
struct Struct1 {
let myLet = "my let"
let myLet2: Bool
let myLet3: String
}
let s1 = Struct1(myLet2: false, myLet3: "My url123")
The error is: missing argument for parameter myLet. Why is that?...
46
votes
6
answers
30k
views
How to I import 3rd party frameworks into Xcode Playground?
How do I import 3rd part frameworks into Xcode Playground?
Swift Playground obviously has a framework import mechanism because we can import Cocoa, SpriteKit, and in an OSX Playground, XCPlayground (...
46
votes
1
answer
62k
views
Is there a command-line interface to list all Bluetooth devices in range on mac? [closed]
I want to list all in-range Bluetooth devices, ideally with hostnames, but MAC addresses would be sufficient. I need a command that performs an active scan so I can poll to see if my device is nearby,...
45
votes
5
answers
28k
views
Xcode Swift code folding/collapse
I know we can use "Shift + Alt + Cmd + Left" to fold/collapse swift code in Xcode, but can anyone tell me how to fold/collapse for functions only? Not properties? Or any alternative way?
The default ...
45
votes
4
answers
39k
views
Define macOS window size using SwiftUI
I'm using SwiftUI to develop a new macOS application and can't figure out how to define the window size. In the AppDelegate, the window size is defined as shown below:
// --- AppDelegate.swift ---
...
45
votes
6
answers
17k
views
How can I programmatically find Swift's version?
I know I can find the version of Swift I'm running right now reverting to a Terminal and typing:
xcrun swift --version
Swift version 1.1 (swift-600.0.57.4)
Target: x86_64-apple-darwin13.4.0
Also, I'...
43
votes
5
answers
27k
views
Swift - Capture keydown from NSViewController
I would like to capture keyevents in my little app.
What I have done:
class ViewController : NSViewController {
...
override func keyDown(theEvent: NSEvent) {
if theEvent.keyCode == 124 {
...
42
votes
6
answers
33k
views
Easiest way to force a crash in Swift
What is the easiest way to force a crash in Swift?
I would like to use only one line of code (something that I can add quickly).
I don't want to use breakpoints, I actually want the app to crash.
42
votes
2
answers
19k
views
How to override localizedDescription for custom Error in Swift 3? [duplicate]
Error protocol has only one property localizedDescription.
I tried to create custom object inherited from NSObject and Error but I can not override localizedDescription. How can I do that?
This code ...
42
votes
5
answers
27k
views
Swift String escaping when serializing to JSON using Codable
I'm trying to serialize my object as following:
import Foundation
struct User: Codable {
let username: String
let profileURL: String
}
let user = User(username: "John", profileURL: "http://...
42
votes
9
answers
20k
views
How to run an iOS app that causes runtime error for frameworks "code signature invalid"
Every time I try to run this application it builds OK and installs, but as soon as it runs, the application breaks and prints this error message:
dyld: Library not loaded: @rpath/StandardCyborgFusion....
41
votes
3
answers
28k
views
quitting xcode cocoa swift app
I have written my first swift OS/X application in XCode 6. It all works except I cannot figure out how to exit the app. I have a button to exit and the ExitNow function defined as follows:
@IBAction ...
41
votes
4
answers
22k
views
Keep window always on top?
In Objective-C for Cocoa Apps it's possible to use such way to keep window always on top?
How to achieve the same with Swift?
self.view.window?.level = NSFloatingWindowLevel
Causes build error Use ...
39
votes
12
answers
39k
views
Get currency symbols from currency code with swift
How can I get the currency symbols for the corresponding currency code with Swift (macOS).
Example:
EUR = €1.00
USD = $1.00
CAD = $1.00
GBP = £1.00
My code:
var formatter = NSNumberFormatter()
...
39
votes
4
answers
52k
views
iOS temporary folder location
My app has just been rejected by Apple because it was storing temporary or cache files in the documents directory. Right. Their rejection message states:
Temporary files used by your app should ...
39
votes
3
answers
11k
views
Swift 2.0 minimum system version requirement (deployment target)
So Swift 2.0 is coming up with Xcode 7, what is the minimum iOS / OS X system version required to use Swift 2.0?
38
votes
11
answers
37k
views
CommonHMAC in Swift
I'm trying to create a HMAC SHA-1 hash of a string in Swift but can't figure out how to interact with the APIs as it doesn't seem to be importing the CommonCrypto framework. I've tried various ...
38
votes
3
answers
16k
views
How to create Status Bar icon and menu in macOS using SwiftUI
What is SwiftUI API for creating status bar menus?
Apple seems to use SwiftUI views in Battery & WiFi menus according to the accessibility inspector. Screenshot of a battery menu attached, also ...
37
votes
5
answers
7k
views
SwiftUI – Multi column TableView in macOS
I have been experimenting a bit with SwiftUI and can’t seem to find any information at all about how to use SwiftUI to create a multi column TableView as in NSTableView. All the examples I have ...
36
votes
8
answers
24k
views
Setting an NSManagedObject relationship in Swift
How does one add an object to a relationship property in an NSManagedObject subclass in Swift?
In Objective-C, when you generate an NSManagedObject subclass in Xcode from the data model, there's an ...
36
votes
10
answers
17k
views
How to monitor a folder for new files in swift?
How would I monitor a folder for new files in swift, without polling (which is very inefficient)? I've heard of APIs such as kqueue and FSEvents - but I'm not sure it's possible to implement them in ...
36
votes
1
answer
3k
views
New auto layout errors with Xcode 8.1
I have a macOS app that was running just fine until I upgraded to Xcode 8.1.
The app opens a dialogue with NSOpenPanel to allow the user to choose an image. When I select an image and choose "Open" I ...
35
votes
4
answers
8k
views
In Swift, how do I avoid both optionals and nil object references?
The whole reason for optionals is to prevent runtime crashes resulting from hitting a variable assigned to nil/null/none. Therefore, variables can't be nil; instead they can be wrapped in an Optional ...