Getting value of variable from a class to another place - swift3

Let's say:
I have class String containing an empty string variable a
In page 1, I need to set variable a = "string here"
In page 2, I need to call the value of a
How can I do that?

from page 1
let nextview = self.storyboard?.instantiateViewController(withIdentifier: "Page2Identifier") as! Page1Controller
nextview.prevscreen = a
self.navigationController?.pushViewController(nextview, animated: true)
in Page 2
var prevscreen = ""

Related

Real-time NSTextField formatter in SwiftUI context

After lots of trial and error, I ended up with the following implementation to do real-time formatting for numeric entry in a text field. Various attempts to use a SwiftUI TextField() resulted in many anomalies. The approach below seems to be solid but even here I struggled with the proper approach to sub-classing NSTextField as I couldn't find any documentation on how to handle the designated initializer such that it would be compatible with SwiftUI's frame modifier.
The one minor remaining anomaly is that when placing the cursor in the middle of an entered number then typing non-numeric characters, the cursor advances even though no changes occur in the text. This is livable but I would prefer to keep that from happening.
Is there a better, more "proper" way to implement this?
import Foundation
import SwiftUI
struct NumberField : NSViewRepresentable {
typealias NSViewType = NumberText
var defaultText : String
var maxDigits : Int
var numberValue : Binding<Int>
func makeNSView(context: Context) -> NSViewType {
// Create text field
let numberTextField = NumberText()
numberTextField.isEditable = true
// numberTextField.numberBinding = numberValue
numberTextField.configure(text: defaultText, digits: maxDigits, intBinding: numberValue)
return numberTextField
}
func updateNSView(_ nsView: NSViewType, context: Context) {
// nsView.stringValue = "This is my string"
}
}
/// NumberText draws an NSTextField that will accept only digits up to a maximum number specified when calling Configure. Apple implements some nice integration between SwiftUI's frame and padding modifiers and the NSTextField's designated initializer. Rather than having to figure out how to fix/preserve this integration, this class provides a "configure()" function that is effectively it's initializer. As a result, it is MANDATORY that this class's configure() function be called immediately after initializing the class.
class NumberText : NSTextField {
// Code below jumps through a couple of hoops to avoid having to write a custom initializer since that gets in the middle of Apple's configuration of the text field using standard SwiftUI modifiers.
// NOTE THAT A USER OF NumberText MUST CALL CONFIGURE() IMMEDIATELY AFTER CREATING IT
var numberBinding : Binding<Int> = Binding( // This is initialized with a garbage Binding just to avoid having to write an initializer
get: {return -1},
set: {newValue in return}
)
var defaultText = "Default String"
var maxDigits = 9
private var decimalFormatter = NumberFormatter()
func configure(text: String, digits: Int, intBinding: Binding<Int>) { // Configure is used here instead of attempting to override init()
// Configure values
decimalFormatter.numberStyle = .decimal
defaultText = text
self.placeholderString = defaultText
maxDigits = digits
numberBinding = intBinding
// Set up TextField values
self.integerValue = numberBinding.wrappedValue
if self.integerValue == 0 {self.stringValue = ""}
}
override func textDidChange(_ notification: Notification) {
self.stringValue = numberTextFromString(self.stringValue)
if self.stringValue == "0" {self.stringValue = ""}
}
func numberTextFromString(_ inputText: String, maxLength: Int = 9) -> String {
// Create a filtered and trucated version of inputText
let filteredText = inputText.filter { character in
character.isNumber
}
let truncatedText = String(filteredText.suffix(maxLength))
// Make a number from truncated text
let myNumber = Int(truncating: decimalFormatter.number(from: truncatedText) ?? 0 )
// Set binding value
numberBinding.wrappedValue = myNumber
// Create formatted string for return
let returnValue = decimalFormatter.string(from: myNumber as NSNumber) ?? "?"
return returnValue
}
After some additional trial and error, I was able to fix the cursor problems mentioned in my initial question. The version here is, to the best of my knowledge, bullet proof (though the test team will have a whack at it so perhaps it will change).
Would still welcome any improvement suggestions.
import Foundation
import SwiftUI
struct NumberField : NSViewRepresentable {
typealias NSViewType = NumberText
var defaultText : String
var maxDigits : Int
var numberValue : Binding<Int>
func makeNSView(context: Context) -> NSViewType {
// Create text field
let numberTextField = NumberText()
numberTextField.isEditable = true
numberTextField.configure(text: defaultText, digits: maxDigits, intBinding: numberValue)
return numberTextField
}
func updateNSView(_ nsView: NSViewType, context: Context) {
}
}
/// NumberText draws an NSTextField that will accept only digits up to a maximum number specified when calling Configure. Apple implements some nice integration between SwiftUI's frame and padding modifiers and the NSTextField's designated initializer. Rather than having to figure out how to fix/preserve this integration, this class provides a "configure()" function that is effectively it's initializer. As a result, it is MANDATORY that this class's configure() function be called immediately after initializing the class.
class NumberText : NSTextField {
// Code below jumps through a couple of hoops to avoid having to write a custom initializer since that gets in the middle of Apple's configuration of the text field using standard SwiftUI modifiers.
// NOTE THAT A USER OF NumberText MUST CALL CONFIGURE() IMMEDIATELY AFTER CREATING IT
// The following variable declarations are all immediately initialized to avoid having to write an init() function
var numberBinding : Binding<Int> = Binding( // This is initialized with a garbage Binding just to avoid having to write an initializer
get: {return -1},
set: {newValue in return}
)
var defaultText = "Default String"
var maxDigits = 9
private var decimalFormatter = NumberFormatter()
func configure(text: String, digits: Int, intBinding: Binding<Int>) { // Configure is used here instead of attempting to override init()
// Configure values
decimalFormatter.numberStyle = .decimal
defaultText = text
self.placeholderString = defaultText
maxDigits = digits
numberBinding = intBinding
// Make sure that default text is shown if numberBinding.wrappedValue is 0
if numberBinding.wrappedValue == 0 {self.stringValue = ""}
}
override func textDidChange(_ notification: Notification) {
self.stringValue = numberTextFromString(self.stringValue, maxLength: maxDigits) // numberTextFromString() also sets the wrappedValue of numberBinding
if self.stringValue == "0" {self.stringValue = ""}
}
/// Takes in string from text field and returns the best number string that can be made from it by removing any non-numeric characters and adding comma separators in the right places.
/// Along the way, self.numberBinding.warppedValue is set to the Int corresponding to the output string and self's cursor is reset to account for the erasure of invalid characters and the addition of commas
/// - Parameters:
/// - inputText: Incoming text from text field
/// - maxLength: Maximum number of digits allowed in this field
/// - Returns:String representing number
func numberTextFromString(_ inputText: String, maxLength: Int) -> String {
var decrementCursorForInvalidChar = 0
var incomingDigitsBeforeCursor = 0
// For cursor calculation, find digit count behind cursor in incoming string
// Get incoming cursor location
let incomingCursorLocation = currentEditor()?.selectedRange.location ?? 0
// Create prefix behind incoming cursor location
let incomingPrefixToCursor = inputText.prefix(incomingCursorLocation)
// Count digits in prefix
for character in incomingPrefixToCursor {
if character.isNumber == true {
incomingDigitsBeforeCursor += 1
}
}
// Create a filtered and trucated version of inputText
var characterCount = 0
let filteredText = inputText.filter { character in
characterCount += 1
if character.isNumber == true {
return true
} else { // character is invalid or comma.
if character != "," { // character is invalid,
if characterCount < inputText.count { // Only decrement cursor if not at end of string
// Decrement cursor
decrementCursorForInvalidChar += 1
}
}
return false
}
}
// Decrement cursor as needed for invalid character entries
currentEditor()!.selectedRange.location = incomingCursorLocation - decrementCursorForInvalidChar
let truncatedText = String(filteredText.prefix(maxLength))
// Make a number from truncated text
let myNumber = Int(truncating: decimalFormatter.number(from: truncatedText) ?? 0 )
// Set binding value
numberBinding.wrappedValue = myNumber
// Create formatted string for return
let outgoingString = decimalFormatter.string(from: myNumber as NSNumber) ?? "?"
// For cursor calculation, find character representing incomingDigitsBeforeCursor.lastIndex
var charCount = 0
var digitCount = 0
var charIndex = outgoingString.startIndex
while digitCount < incomingDigitsBeforeCursor && charCount < outgoingString.count {
charIndex = outgoingString.index(outgoingString.startIndex, offsetBy: charCount)
charCount += 1
if outgoingString[charIndex].isNumber == true {
digitCount += 1
}
}
// Get integer corresponding to current charIndex
let outgoingCursorLocation = outgoingString.distance(from: outgoingString.startIndex, to: charIndex) + 1
currentEditor()!.selectedRange.location = outgoingCursorLocation
return outgoingString
}
}

Swift2 to Swift3 error - Cannot assign value of type 'NSDictionary?' to type 'AddressModel'

Moving an app from Swift2 to Swift3 and I've hit an error that I've been unable to fix after trying several different suggestions.
lazy var address: AddressModel? = {
[unowned self] in
var dict = self.getpayloadDict()
var model: AddressModel
model = dict
return model
}()
model = dict throws Cannot assign value of type 'NSDictionary?' to type 'AddressModel'
The AddressModel . . .
class AddressModel: Deserializable {
var City: String?
var State: String?
var PostalCode: String?
required init(data: [String: AnyObject]) {
City = data["City"] as! String?
State = data["State"] as! String?
PostalCode = data["PostalCode"] as! String?
}
}
Any help appreciated.
The error is supposed to occur also in Swift 2. It's pretty clear: getpayloadDict() returns a dictionary which doesn't match AddressModel.
You might create an AddressModel instance from the dictionary
lazy var address: AddressModel? = { // this closure does not cause a retain cycle
let dict = self.getpayloadDict()
return AddressModel(data: dict)
}()
Side note:
as! String? (force unwrap an optional to an optional) is horrible syntax. Use regular conditional downcast as? String. And please conform to the naming convention that variable names start with a lowercase letter.

Argument of '#selector' does not refer

Anyone can help me?
This is my code:
fileprivate func datapicker(sender : UITextField){
let datePickerView:UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.date
sender.inputView = datePickerView
datePickerView.addTarget(self, action: #selector(datePickerValueChanged(sender: datePickerView, myText: sender)), for: UIControlEvents.valueChanged)
}
#objc func datePickerValueChanged(sender:UIDatePicker , myText : UITextField) {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.medium
dateFormatter.timeStyle = DateFormatter.Style.none
myText.text = dateFormatter.string(from: sender.date)
}
#IBAction func fromdateAction(_ sender: UITextField) {
datapicker(sender: sender)
}
Selectors can't be called like that. You don't get to pass the arguments for a selector, the API does.
You have to pass in:
#selector(datePickerValueChanged)
This means that your datePickerValueChanged method can't take 2 arguments, because the API expects only one.
This means that you need a way to know which text field's date picker changed. One simple way to do this is to create a property called focusedTextField:
var focusedTextField: UITextField!
Set this to sender in your dataPicker method:
focusedTextField = sender
And set it to nil when the user ends editing
// in another method that is called when the text field ends editing
focusedTextField = nil
Now, you can remove the second argument from datePickerValueChanged and use focusedTextField instead.

Swift 3 Change color of selected text in UITextView

I have just changed my code to Swift 3 from swift 2.3. The following code is working fine with swift 2.3 but there is no effect with swift 3.
I want to develop a customise text editor. And let user to select text and change its color. For that i am using this code.
let selectedRange = textView.selectedRange
let currentAttr = textView.textStorage.attributes(at: selectedRange.location, effectiveRange: nil)
var attrName = NSForegroundColorAttributeName
if !isForeGround{
attrName = NSBackgroundColorAttributeName
}
if currentAttr[attrName] == nil || currentAttr[attrName] as! UIColor != selectedUIColor{
let dict = [attrName:selectedUIColor]
let currentFont = currentAttr[NSFontAttributeName]
let fontDescriptor = (currentFont! as AnyObject).fontDescriptor
let updatedFont = UIFont(descriptor: fontDescriptor!, size: 0.0)
let dict2 = [NSFontAttributeName: updatedFont]
textView.textStorage.beginEditing()
if currentAttr.count>0{
textView.textStorage.addAttributes(dict, range: selectedRange)
}else{
textView.textStorage.setAttributes(dict, range: selectedRange)
}
textView.textStorage.addAttributes(dict2, range: selectedRange)
textView.textStorage.endEditing()
}
Runs successfully but there is no effect on text color.
This worked for me.
First you need to catch your selected text, with:
let selectedText = textView.selectedRange
Then, you create the attribute:
let myAttribute = [ NSForegroundColorAttributeName: selectedUIColor]
And lastly:
textView.textStorage.addAttributes(myAttribute, range: selectedText)
This must be in an Action called by any sender
Hope it works also for you!
Regards
Selected text can be changed with
self.textView.tintColor = .red

Migration from swift2 to swift3 and changes for advanceBy for Range<String.Index>

There is a function
func find(... rangeString1: Range<String.Index>, ...) ...
and this function has in swift2
while i >= minimalIdentity{
var first = rangeString1.startIndex
var last = first.advancedBy(i, limit: rangeString1.endIndex)
...
Question is how should look the
var last
in swift3
var first = rangeString1.lowerBound
var last =
Whatever string those indices belong to is responsible for this in Swift 3:
let first = rangeString1.lowerBound
let last = myString.index(first, offsetBy: i, limit: rangeString1.upperBound)
Just use endIndex:
var last = rangeString1.endIndex