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 let attributedString = NSMutableAttributedString(string: text) text.enumerateSubstringsInRange(textRange, options: NSStringEnumerationOptions.ByWords, { (substring, substringRange, enclosingRange, stop) -> () in if (substring == “saying”) { attributedString.addAttribute(NSForegroundColorAttributeName, value: NSColor.redColor(), range: substringRange) } }) … Read more

SwiftUI: How to implement a custom init with @Binding variables

I’m working on a money input screen and need to implement a custom init to set a state variable based on the initialized amount. I thought this would work, but I’m getting a compiler error of: Cannot assign value of type ‘Binding<Double>’ to type ‘Double’ struct AmountView : View { @Binding var amount: Double @State … Read more

How do I concatenate strings in Swift?

How to concatenate string in Swift? In Objective-C we do like NSString *string = @”Swift”; NSString *resultStr = [string stringByAppendingString:@” is a new Programming Language”]; or NSString *resultStr=[NSString stringWithFormat:@”%@ is a new Programming Language”,string]; But I want to do this in Swift-language. 21 Answers 21

How to set Status Bar Style in Swift 3

I’m using Xcode 8.0 beta 4. In previous version, UIViewController have method to set the status bar style public func preferredStatusBarStyle() -> UIStatusBarStyle However, I found it changed to a “Get ONLY varaiable” in Swift 3. public var preferredStatusBarStyle: UIStatusBarStyle { get } How can provide the style to use in my UIViewController? 33 Answers … Read more

Make a VStack fill the width of the screen in SwiftUI

Given this code: import SwiftUI struct ContentView: View { var body: some View { VStack(alignment: .leading) { Text(“Title”) .font(.title) Text(“Content”) .lineLimit(nil) .font(.body) Spacer() } .background(Color.red) } } #if DEBUG struct ContentView_Previews : PreviewProvider { static var previews: some View { ContentView() } } #endif It results in this interface: How can I make the VStack … Read more

structure vs class in swift language

From Apple book “One of the most important differences between structures and classes is that structures are always copied when they are passed around in your code, but classes are passed by reference.” Can anyone help me understand what that means? To me, classes and structs seem to be the same. 13 Answers 13