Swift 2 Struct with Array - ios

I am new to Swift and to making games (as well as fairly new to coding in general), I currently want to pass a variable between two Gamescenes. After doing some research I have come to the conclusion I require a struct with a [int]() to hold the numbers I want for some reason while I am not able to assign values into the my integer array generatedSequenceArray
my Struct:
struct generatedSequenceForGameScene {
static var generatedSequenceArray = [Int]()
}
In my one of my GameScene classes I have initialized my generatedSequenceForGameScene struct object:
var generatedSequence = generatedSequenceForGameScene()
The method I am calling on is:
func pickNumbers(){
for var x = 0; x < highLevel; ++x{
generatedSequence.generatedSequenceArray.append(Int(arc4random_uniform(10)))
print(generatedSequence.generatedSequenceArray)
}
}
Before attempting to have a variable move from each GameScene my code was this:
Variable:
var generatedSequence = [Int]()
Method:
func pickNumbers(){
for var x = 0; x < highLevel; ++x{
generatedSequence.append(Int(arc4random_uniform(10)))
print(generatedSequence)
}
}
This gave me my desired results.
NOTE: The pickNumber method gets called in a method named loadView() which is placed in the didMoveToView method in the program

There are several things to note here.
First, the static keyword inside your struct makes generatedSequenceArray a static property. This means it'll be the same everywhere you call it. This isn't what you want.
Second, unless you plan to add more properties to your struct, there's no reason for it to exist. Why wrap it in a struct at all? Why not just pass the array of integers (your picked numbers) directly?
Third, type names should be proper case. So generatedSequenceForGameScene should be GeneratedSequenceForGameScene).

I'm not sure what exactly isn't working for you. I guess you're having trouble sharing a sequence from one game scene to another. If that's the problem, maybe the following helps:
struct Sequence {
var seqArray = [Int]()
mutating func pickNumbers() {
for var x = 0; x < 10; ++x {
seqArray.append(Int(arc4random_uniform(10)))
}
print( "picked: \(seqArray)" )
}
}
class GameScene {
var s = Sequence()
}
var g1 = GameScene()
var g2 = GameScene()
g1.s.pickNumbers()
g2.s.seqArray = g1.s.seqArray
print( g2.s.seqArray )

Related

Swift 3, accessing user input Text Field values from ViewControler and used them in a model .swift file

This is my first question. I'm new to swift and programming in general so please don't laugh if I'm asking stupid question :)
So I hava a SettingsViewControler where users sets their values, lets say they set the temperature value. What i'm trying to do, is to take that temperature value that they input and pass it to my model.swift file, to introduce that value in the formula, to calculate the new value with the input temperature. I hope this make sense.
Is there a way to do that directly by calling the class form VC to the newData class that I created in model.swift file, or I should use some methods like UserDefaults to pass data.
Here is the code example:
First I created a Settings.swift file
// Settings.swift file
import Foundation
class Settings {
var inputTemperature: Float = 0
init(inputTemperature: Float) {
self.inputTemperature = inputTemperature
}
}
Here is the Settings View Controller
//Settings ViewCOntroller. swift file
import UIKit
class SettingsViewController: UIViewController {
#IBOutlet weak var inputTemperatureTextField: UITextField!
var getTemp = Settings(inputTemperature: 72)
override func viewDidLoad() {
super.viewDidLoad()
}
func getValues() {
getTemp.inputTemperature = (inputTemperatureTextField.text! as NSString).floatValue
}
}else if textField == inputTemperatureTextField {
textField.resignFirstResponder()
getValues()
}
So now I have another Calculations.swift file where I want to get the inputTemperature value to use it in the formula
//Calculation. swift file
import Foundation
class Calculations {
var inputA: Float = 0
var inputB: Float = 0
var resultC: Float = 0
init(inputA: Float, inputB: Float) {
self.inputA = inputA
self.inputB = inputB
}
// Here i want to add the temperature value
func calc() {
resutC = inputA * inputC // * inputTemperature
}
I want the get (inputTemperatureTextField.text! as NSString).floatValue value from SettingsView COntroller to introduce it in the formula located Calculation.swift file
Thanks
You should really post some of your code to give us some insight in what exactly it is you want.
Lets say in your model you have a temperature value like
var temp: Int?
Then you can initialize it in your VC, and access the temp value
var model = Model()
let model.temp = inputTextField.text
If you are using model.swift for calculation. You will create an object of the model-class in the View controller, after creating an object you can pass the test field value to the model.
var modelObject: model? = model();
modelObject.temprature = txtTemprature.text;

How to keep the reference to an array after manipulation?

How do I keep the reference to an array after an items is appended?
Updated code example, because the prior example didn't seem to be clear enough.
class ViewController: UIViewController {
var numbers = Numbers.singleton.numbers
override func viewDidLoad() {
print(numbers.count)
Numbers.singleton.add(1)
print(numbers.count) // prints 0
print(Numbers.singleton.numbers.count) // prints 1
}
}
class Numbers {
static let singleton = Numbers()
var numbers: [Int]!
private init() {
numbers = []
}
func add(number: Int) {
numbers.append(number)
}
}
Arrays in Swift don't have "references". They are structs, and a struct is a value type. Your (badly named) arrayRef is a separate copy, not a reference to self.array.
Moreover, there is no good reason to want to do what you (seem to) want to do. To have two simultaneous references to a mutable array would be unsafe, since the array can be changed behind your back. The Swift design is sensible; use it, don't subvert it.

Accessing singleton object in swift

This is my first time implementing a singleton to share an instance of an object in swift. Everything seems to be working totally fine, except for when I try and add an element to an array (accessing from another class) that lives within my singleton object. It indeed does not append any objects to the array at all. I'm thinking that it's appending onto an array, but not the same instance of the class that I would like it to be (as I only want one and only one instance). However, If I append elements onto the array from the init() of the class, everything works out just fine. Here's some code (I've simplified all the classes to make things more obvious):
File 1:
class Brew: NSObject {
var method = Method()
//Singleton variable
private static var currentBrew: Brew?
//Method to get the current (and only) brew object
static func getCurrentBrew() -> Brew {
if currentBrew == nil {
currentBrew = Brew()
}
return currentBrew!
}
}
struct Method {
var chemex = Device()
init() {
//If I append here - everything works fine
//chemex.instructions.append = (Instruction(title: "Prepare", direction: "Prewet & Heat", time: 3, water: 0))
}
}
struct Device {
var instructions = [Instruction]()
init() {
instructions.append(Instruction(title: "None", direction: "None", time: 1, water: 0, index: 0))
}
File 2: (where I would like to append to the array of instructions)
let brew = Brew.getCurrentBrew() //How i'm accessing the object
//I'm calling this method from viewDidLoad to set up the array
func setupBrewDevices() {
//This is the line that does not actually append to the singleton instance
brew.method.chemex.instructions.append(Instruction(title: "Extraction", direction: "Match water.", time: 8 , water: 25))
Just a side note, I also tried to make a method that would append an instruction onto the array that lives inside of the same class, but that had the same result. Hopefully this is clear enough - I appreciate any help!
Thanks,
Cole
There is a better way to create a singleton instance in Swift.
class Brew: NSObject {
static let currentBrew = Brew()
var method = Method()
}
This is thread-safe and avoids using an optional.
That said, when I tried your code the instructions array ended up with two elements like I would expect ("None") and ("Extraction"). The problem may lie elsewhere in your code.

Error on Static Array in Swift Class

I am developing an app using swift. I create a subclass from SCNNode for this:
class Charge: SCNNode {
static var tagCounter = 0 //to give every charge a unique tag
static var charges = [Charge]() //to have a pointer that can access all charges
static var selectedCharge: Charge? //pointer to selected charge
override init() {
super.init()
super.geometry = Charge.carbonAtom()
Charge.tagCounter++
self.chargeTag = Charge.tagCounter
Charge.charges.append(self)
}
}
Then after initiating the class several times in ViewController, I want to access them by using Charge.charges. But for some unknown reason, only the last instance is available, and it occupies Charge.charges[0].
I tried to track the object movement by adding property in ViewController var test = [Charge]() and call test.append(charge) every time I initiate charge in ViewController. Then, when I want to access all the charges, Charge.charges loses most of its charges but test does not! Can anyone enlighten me on this? Or is it a bug in Xcode?
n.b. I use debugging tool to track this problem. It turns out that Charge.charges loses its first content as soon as the second initialization is finished, but the first content still exists right after the execution of Charge.charges.append(self)
edit: carbonAtom function
class func carbonAtom() -> SCNGeometry {
let carbonAtom = SCNSphere(radius: 0.8)
carbonAtom.firstMaterial!.diffuse.contents = UIColor.redColor()
carbonAtom.firstMaterial!.specular.contents = UIColor.whiteColor()
return carbonAtom
}
I have just tested, there is not any Xcode bug.
class Charge: NSObject {
static var tagCounter = 0 //to give every charge a unique tag
static var charges = [Charge]() //to have a pointer that can access all charges
override init() {
super.init()
// super.geometry = Charge.carbonAtom()
Charge.tagCounter++
// self.chargeTag = Charge.tagCounter
Charge.charges.append(self)
}
}
Create 3 Changes instance:
for var i = 0; i < 3; i++ {
_ = Charge()
}
print(Charge.charges.count)
The console prints 3.
Try to check your Charge.carbonAtom() static method. I doubt it clear value of the charges array.
OKAY GUYS I FOUND IT!
There are infos that I dont provide (I thought it was irrelevant) in Charge class:
override func removeFromParentNode() {
super.removeFromParentNode()
for var i = 0; i<Charge.charges.count; i++ {
let charge = Charge.charges[i]
if Charge.selectedCharge != nil {
if charge == Charge.selectedCharge! {
Charge.charges.removeAtIndex(i)
}
}
break
}
}
and in ViewController
#IBAction func addCharge(sender: AnyObject) {
let charge = Charge()
scene.rootNode.addChildNode(charge) //root of problem
Charge.selectedCharge = charge
print(Charge.charges.count)
}
what happen is, in the line scene.rootNode.addChildNode(charge), the method automatically calls removeFromParentNode() from charge. Because of that, the charge pointed by Charge.selectedCharge will be removed from Charge.charges when the second charge is initialized.
I try to search for this info in Apple documentation but no avail. If only they document it more comprehensively :/
Thank you for the help guys :) the divide and conquer method I use to replicate the bug really helps narrowing down the problem

What if I want to assign a property to itself?

If I attempt to run the following code:
photographer = photographer
I get the error:
Assigning a property to itself.
I want to assign the property to itself to force the photographer didSet block to run.
Here's a real-life example: In the "16. Segues and Text Fields" lecture of the Winter 2013 Stanford iOS course (13:20), the professor recommends writing code similar to the following:
#IBOutlet weak var photographerLabel: UILabel!
var photographer: Photographer? {
didSet {
self.title = photographer.name
if isViewLoaded() { reload() }
}
}
override func viewDidLoad() {
super.viewDidLoad()
reload()
}
func reload() {
photographerLabel.text = photographer.name
}
Note: I made the following changes: (1) the code was switched from Objective-C to Swift; (2) because it's in Swift, I use the didSet block of the property instead of the setPhotographer: method; (3) instead of self.view.window I am using isViewLoaded because the former erroneously forces the view to load upon access of the view property; (4) the reload() method (only) updates a label for simplicity purposes, and because it resembles my code more closely; (5) the photographer IBOutlet label was added to support this simpler code; (6) since I'm using Swift, the isViewLoaded() check no longer exists simply for performance reasons, it is now required to prevent a crash, since the IBOutlet is defined as UILabel! and not UILabel? so attempting to access it before the view is loaded will crash the application; this wasn't mandatory in Objective-C since it uses the null object pattern.
The reason we call reload twice is because we don't know if the property will be set before or after the view is created. For example, the user might first set the property, then present the view controller, or they might present the view controller, and then update the property.
I like how this property is agnostic as to when the view is loaded (it's best not to make any assumptions about view loading time), so I want to use this same pattern (only slightly modified) in my own code:
#IBOutlet weak var photographerLabel: UILabel?
var photographer: Photographer? {
didSet {
photographerLabel?.text = photographer.name
}
}
override func viewDidLoad() {
super.viewDidLoad()
photographer = photographer
}
Here instead of creating a new method to be called from two places, I just want the code in the didSet block. I want viewDidLoad to force the didSet to be called, so I assign the property to itself. Swift doesn't allow me to do that, though. How can I force the didSet to be called?
Prior to Swift 3.1 you could assign the property name to itself with:
name = (name)
but this now gives the same error: "assigning a property to itself".
There are many other ways to work around this including introducing a temporary variable:
let temp = name
name = temp
This is just too fun not to be shared. I'm sure the community can come up with many more ways to do this, the crazier the better
class Test: NSObject {
var name: String? {
didSet {
print("It was set")
}
}
func testit() {
// name = (name) // No longer works with Swift 3.1 (bug SR-4464)
// (name) = name // No longer works with Swift 3.1
// (name) = (name) // No longer works with Swift 3.1
(name = name)
name = [name][0]
name = [name].last!
name = [name].first!
name = [1:name][1]!
name = name ?? nil
name = nil ?? name
name = name ?? name
name = {name}()
name = Optional(name)!
name = ImplicitlyUnwrappedOptional(name)
name = true ? name : name
name = false ? name : name
let temp = name; name = temp
name = name as Any as? String
name = (name,0).0
name = (0,name).1
setValue(name, forKey: "name") // requires class derive from NSObject
name = Unmanaged.passUnretained(self).takeUnretainedValue().name
name = unsafeBitCast(name, to: type(of: name))
name = unsafeDowncast(self, to: type(of: self)).name
perform(#selector(setter:name), with: name) // requires class derive from NSObject
name = (self as Test).name
unsafeBitCast(dlsym(dlopen("/usr/lib/libobjc.A.dylib",RTLD_NOW),"objc_msgSend"),to:(#convention(c)(Any?,Selector!,Any?)->Void).self)(self,#selector(setter:name),name) // requires class derive from NSObject
unsafeBitCast(class_getMethodImplementation(type(of: self), #selector(setter:name)), to:(#convention(c)(Any?,Selector!,Any?)->Void).self)(self,#selector(setter:name),name) // requires class derive from NSObject
unsafeBitCast(method(for: #selector(setter:name)),to:(#convention(c)(Any?,Selector,Any?)->Void).self)(self,#selector(setter:name),name) // requires class derive from NSObject
_ = UnsafeMutablePointer(&name)
_ = UnsafeMutableRawPointer(&name)
_ = UnsafeMutableBufferPointer(start: &name, count: 1)
withUnsafePointer(to: &name) { name = $0.pointee }
//Using NSInvocation, requires class derive from NSObject
let invocation : NSObject = unsafeBitCast(method_getImplementation(class_getClassMethod(NSClassFromString("NSInvocation"), NSSelectorFromString("invocationWithMethodSignature:"))),to:(#convention(c)(AnyClass?,Selector,Any?)->Any).self)(NSClassFromString("NSInvocation"),NSSelectorFromString("invocationWithMethodSignature:"),unsafeBitCast(method(for: NSSelectorFromString("methodSignatureForSelector:"))!,to:(#convention(c)(Any?,Selector,Selector)->Any).self)(self,NSSelectorFromString("methodSignatureForSelector:"),#selector(setter:name))) as! NSObject
unsafeBitCast(class_getMethodImplementation(NSClassFromString("NSInvocation"), NSSelectorFromString("setSelector:")),to:(#convention(c)(Any,Selector,Selector)->Void).self)(invocation,NSSelectorFromString("setSelector:"),#selector(setter:name))
var localVarName = name
withUnsafePointer(to: &localVarName) { unsafeBitCast(class_getMethodImplementation(NSClassFromString("NSInvocation"), NSSelectorFromString("setArgument:atIndex:")),to:(#convention(c)(Any,Selector,OpaquePointer,NSInteger)->Void).self)(invocation,NSSelectorFromString("setArgument:atIndex:"), OpaquePointer($0),2) }
invocation.perform(NSSelectorFromString("invokeWithTarget:"), with: self)
}
}
let test = Test()
test.testit()
There are some good workarounds but there is little point in doing that.
If a programmer (future maintainer of the code) sees code like this:
a = a
They will remove it.
Such a statement (or a workaround) should never appear in your code.
If your property looks like this:
var a: Int {
didSet {
// code
}
}
then it's a not a good idea to invoke the didSet handler by assignment a = a.
What if a future maintainer adds a performance improvement to the didSet like this?
var a: Int {
didSet {
guard a != oldValue else {
return
}
// code
}
}
The real solution is to refactor:
var a: Int {
didSet {
self.updateA()
}
}
fileprivate func updateA() {
// the original code
}
And instead of a = a directly call updateA().
If we are speaking about outlets, a suitable solution is to force the loading of views before assigning for the first time:
#IBOutlet weak var photographerLabel: UILabel?
var photographer: Photographer? {
didSet {
_ = self.view // or self.loadViewIfNeeded() on iOS >= 9
photographerLabel?.text = photographer.name // we can use ! here, it makes no difference
}
}
That will make the code in viewDidLoad unnecessary.
Now you might be asking "why should I load the view if I don't need it yet? I want only to store my variables here for future use". If that's what you are asking, it means you are using a view controller as your model class, just to store data. That's an architecture problem by itself. If you don't want to use a controller, don't even instantiate it. Use a model class to store your data.
I hope one day #Swift developers will fix this miscuzzi :)
Simple crutch:
func itself<T>(_ value: T) -> T {
return value
}
Use:
// refresh
style = itself(style)
image = itself(image)
text = itself(text)
(optionals including)
Make a function that the didSet calls then call that function when you want to update something? Seems like this would guard against developers going WTF? in future
#vacawama did a great job with all those options. However in iOS 10.3, Apple banned some of these ways and most likely will be doing it in the future again.
Note: To avoid the risk and future errors, I will use a temporary variable.
We can create a simple function for that:
func callSet<T>(_ object: inout T) {
let temporaryObject = object
object = temporaryObject
}
Would be used like: callSet(&foo)
Or even a unary operator, if there is a fitting one ...
prefix operator +=
prefix func +=<T>(_ object: inout T) {
let temporaryObject = object
object = temporaryObject
}
Would be used like: +=foo

Resources