Skip to content
archive

stack-overflow-swift-2026-06-04

Swift · · 2 min read

Top Swift Questions on Stack Overflow — 2026-06-04

10 top-voted questions.

Passing data between view controllers

Score: 1503 | Answers: 44 | Views: 535044

Tags: ios, objective-c, swift, model-view-controller, uiviewcontroller

I'm new to iOS and Objective-C and the whole MVC paradigm and I'm stuck with the following: I have a view that acts as a data entry form and I want to give the user the option to select multiple products. The products are listed on another view with a UITableViewController and I have enabled multipl


How do I call Objective-C code from Swift?

Score: 1143 | Answers: 17 | Views: 390296

Tags: swift, objective-c

In Swift, how does one call Objective-C code?

Apple mentioned that they could co-exist in one application, but does this mean that one could technically re-use old classes made in Objective-C whilst building new classes in Swift?


How to change Status Bar text color in iOS

Score: 1096 | Answers: 61 | Views: 678854

Tags: ios, swift, ios7, statusbar, uistatusbar

My application has a dark background, but in iOS 7 the status bar became transparent. So I can't see anything there, only the green battery indicator in the corner. How can I change the status bar text color to white like it is on the home screen?


#pragma mark in Swift?

Score: 1022 | Answers: 20 | Views: 258392

Tags: swift, documentation

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?


Swift Beta performance: sorting arrays

Score: 990 | Answers: 9 | Views: 120129

Tags: swift, performance, sorting, xcode6, compiler-optimization

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 = 1000000 var x = [Int](repeating: 0, count: n) for i in 0..<n {


How to iterate a loop with index and element in Swift

Score: 979 | Answers: 21 | Views: 703941

Tags: arrays, swift, enumeration

Is there a function that I can use to iterate over an array and have both index and element, like Python's enumerate? for index, element in enumerate(list): ...


#ifdef replacement in the Swift language

Score: 887 | Answers: 19 | Views: 361578

Tags: swift, xcode, preprocessor, preprocessor-directive

In C/C++/Objective C you can define a macro using compiler preprocessors. Moreover, you can include/exclude some parts of code using compiler preprocessors. #ifdef DEBUG // Debug-only code #endif

Is there a similar solution in Swift?


Get the length of a String

Score: 846 | Answers: 41 | Views: 549228

Tags: swift, string

How do you get the length of a String? For example, I have a variable defined like:

var test1: String = "Scott"

However, I can't seem to find a length method on the string.


Split a String into an array in Swift?

Score: 844 | Answers: 41 | Views: 831338

Tags: arrays, swift, string, split

Say I have a string here: var fullName: String = "First Last"

I want to split the string based on whitespace and assign the values to their respective variables var fullNameArr = // something like: fullName.explode(" ")

var firstName: String = fullNameArr[0] var lastName: Stri


How do I see which version of Swift I'm using?

Score: 761 | Answers: 19 | Views: 325215

Tags: swift, xcode, terminal

I just created a new Swift project within Xcode. I am wondering which version of Swift it's using.

How can I see, in Xcode or the terminal, what version of Swift I am using inside my project?