How to fix "cannot use instance member 'textField_1' within property initializer; property initializers run before 'self' is available"? - initialization

My compiler doesn't show any problems but at runtime, I get an unknown error. I'm trying to place textField string input into a double array. Not sure what is wrong can somebody please explain what I'm doing wrong.
Haven't really tried anything but I think I have to use lazy syntax somehow.
import UIKit
class ViewController: UIViewController {
// Outlets
// textFields
#IBOutlet weak var textField_1: UITextField!
#IBOutlet weak var textField_2: UITextField!
#IBOutlet weak var textField_3: UITextField!
#IBOutlet weak var textField_4: UITextField!
#IBOutlet weak var textField_5: UITextField!
// labels
#IBOutlet weak var label_1: UILabel!
#IBOutlet weak var label_2: UILabel!
#IBOutlet weak var label_3: UILabel!
#IBOutlet weak var label_4: UILabel!
#IBOutlet weak var label5: UILabel!
// Variables
var input : String = "0";
var a = 0
var age = "9.5"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
var numbers : [Double] = [90, 0, 0, 0, 0]; // where textfields will be converted to double
var userinput : [String?] = [textField_1.text, textField_2.text, textField_3.text, textField_4.text, textField_5.text]!; // textField storage
// Button
#IBAction func button ()
{
// loop inserts userinput into numbers array
while (a < 3) {
numbers.insert(convert_to_double(input1: userinput[a]!) ,at: a)// inserts userinput after
a += 1 // stops loop and changes array index
}
}
// converts strings to double
func convert_to_double(input1: String) -> Double
{
let input0 : Double? = Double(input1);
return input0!
}
}
:0: error: cannot use instance member 'textField_1' within property initializer; property initializers run before 'self' is availableenter image description here

You are trying to access the user's input with an index value. Creating an array of text input values has two problems:
In your case, you can't create this array with a property initializer because self is not available until after the class has been fully initialized.
Creating an array of the user inputs will be static and won't show the latest values of the UITextFields if the user updates one before pressing the button.
You can handle this in one of two ways.
Use a computed property:
If you turn userinput into a computed property, then it will always return an array with the latest values:
var userinput : [String?] { return [textField_1.text, textField_2.text, textField_3.text, textField_4.text, textField_5.text] }
The downside is that every access of userinput will recreate this array which is a bit inefficient, but it ensures the array contains the latest values. You can assign this to a local variable at the top of button to avoid recreating the array in the loop:
#IBAction func button ()
{
let userinput = self.userinput
// loop inserts userinput into numbers array
while (a < 3) {
numbers.insert(convert_to_double(input1: userinput[a]!) ,at: a)// inserts userinput after
a += 1 // stops loop and changes array index
}
}
Create a lazy var of the UITextFields:
Create a lazy array of the textFields. This array will be created the first time it is accessed, but since it contains objects, you will always have access to the latest input values from the user.
lazy var textFields = [textField_1!, textField_2!, textField_3!, textField_4!, textField_5!]
and then change your button loop to use textFields[a].text! instead of userinput[a]!:
#IBAction func button ()
{
// loop inserts userinput into numbers array
while (a < 3) {
numbers.insert(convert_to_double(input1: textFields[a].text!) ,at: a)// inserts userinput after
a += 1 // stops loop and changes array index
}
}

Related

SwiftUI - share dictionary among views, unclear what arguments to use at #Main / WindowGroup

I'm trying to build an app (macOS, but would be the same for iOS) that creates a number of grids, the outcome of which is to be shown in a second screen. For this, I'm sharing data across these screens, and I'm running into an issue here, I hope someone can help or point me in the right direction. I'll share a simplified version of the code below (working in Xcode 14.0.1)
The code creates a dictionary that can be shown in a grid, on which calculations can be done. The idea is then to add this grid, with some descriptive variables, into another dictionary
The building blocks of the grid are cells
Import Foundation
struct Cell: Comparable, Equatable, Identifiable, Hashable {
static func == (lhs: Cell, rhs: Cell) -> Bool {
lhs.randomVarOne == rhs.randomVarOne
}
var randomVarOne: Double
var randomVarTwo: Bool
// other vars omitted here
var id: Int { randomVarOne }
static func < (lhs: Cell, rhs: Cell) -> Bool {
return lhs.randomVarOne < rhs.randomVarOne
}
}
this is also where there are a bunch of funcs to calculate next neighbor cells in the grid etc
then the grid is defined in a class:
class Info: ObservableObject, Hashable {
static func == (lhs: Info, rhs: Info) -> Bool {
lhs.grid == rhs.grid
}
func hash(into hasher: inout Hasher) {
hasher.combine(grid)
}
#Published var grid = [Cell]()
var arrayTotal = 900
#Published var toBeUsedForTheGridCalculations: Double = 0.0
var toBeUsedToSetTheVarAbove: Double = 0.0
var rowTotalDouble: Double {sqrt(Double(arrayTotal)) }
var rowTotal: Int {
Int(rowTotalDouble) != 0 ? Int(rowTotalDouble) : 10 }
The class includes a func to create and populate the grid with Cells and add these Cells to the grid var. It also includes the formulas to do the calculations on the grid using a user input. The class did not seem to need an initializer.
This is the Scenario struct:
struct Scenario: Comparable, Equatable, Identifiable, Hashable {
static func == (lhs: Scenario, rhs: Scenario) -> Bool {
lhs.scenarioNumber == rhs.scenarioNumber
}
func hash(into hasher: inout Hasher) {
hasher.combine(scenarioNumber)
}
var scenarioNumber: Int
var date: Date
var thisIsOneSnapshot = [Info]()
var id: Int { scenarioNumber }
static func < (lhs: Scenario, rhs: Scenario) -> Bool {
return lhs.scenarioNumber < rhs.scenarioNumber
}
}
added hashable since it uses the Info class as an input.
Then there is the class showing the output overview
class OutputOverview: ObservableObject {
#Published var snapshot = [Scenario]()
// the class includes a formula of how to add the collection of cells (grid) and the additional variables to the snapshot dictionary. Again no initializer was necessary.
Now to go to the ContentView.
struct ContentView: View {
#Environment(\.openURL) var openURL
var scenarioNumberInput: Int = 0
var timeStampAssigned: Date = Date.now
#ObservedObject private var currentGrid: Info = Info()
#ObservedObject private var scenarios: Combinations = Combinations()
var usedForTheCalculations: Double = 0.0
var rows =
[
GridItem(.flexible()),
// whole list of GridItems, I do not know how to calculate these:
// var rows = Array(repeating: GridItem(.flexible()), count: currentGrid.rowTotal)
//gives error "Cannot use instance member 'currentGrid' within property initializer;
// property iunitializers run before 'self' is available
]
var body: some View {
GeometryReader { geometry in
VStack {
ScrollView {
LazyHGrid(rows: rows, spacing: 0) {
ForEach(0..<currentGrid.grid.count, id :\.self) { w in
let temp = currentGrid.grid[w].varThatAffectsFontColor
let temp2 = currentGrid.grid[w].varThatAffectsBackground
Text("\(currentGrid.grid[w].randomVarOne, specifier: "%.2f")")
.frame(width: 25, height: 25)
.border(.black)
.font(.system(size: 7))
.foregroundColor(Color(wordName: temp))
.background(Color(wordName: temp2))
}
}
.padding(.top)
}
VStack{
HStack {
Button("Start") {
}
// then some buttons to do the calculations
Button("Add to collection"){
scenarios.addScenario(numbering: scenarioNumberInput, timeStamp:
Date.now, collection: currentGrid.grid)
} // this should add the newly recalculated grid to the dictionary
Button("Go to Results") {
guard let url = URL(string: "myapp://scenario") else { return }
openURL(url)
} // to go to the screen showing the scenarios
Then the second View, the ScenarioView:
struct ScenarioView: View {
#State var selectedScenario = 1
#ObservedObject private var scenarios: OutputOverview
var pickerNumbers = [ 1, 2, 3, 4 , 5]
// this is to be linked to the number of scenarios completed,this code is not done yet.
var rows =
[
GridItem(.flexible()),
GridItem(.flexible()),
// similar list of GridItems here....
var body: some View {
Form {
Section {
Picker("Select a scenario", selection: $selectedScenario) {
ForEach(pickerNumbers, id: \.self) {
Text("\($0)")
}
}
}
Section {
ScrollView {
if let idx = scenarios.snapshot.firstIndex(where:
{$0.scenarioNumber == selectedScenario}) {
LazyHGrid(rows: rows, spacing: 0) {
ForEach(0..<scenarios.snapshot[idx].thisIsOneSnapshot.count,
id :\.self) { w in
let temp =
scenarios.snapshot[idx].thisIsOneSnapshot[w].varThatAffectsFontColor
let temp2 =
scenarios.snapshot[idx].thisIsOneSnapshot[w].varThatAffectsBackground
Text("\(scenarios.snapshot[idx].thisIsOneSnapshot[w].randomVarOne, specifier: "%.2f")")
.frame(width: 25, height: 25)
.border(.black)
.font(.system(size: 7))
.foregroundColor(Color(wordName: temp))
.background(Color(wordName: temp2))
}
}
}
}
}
}
}
}
Now while the above does not (for the moment..) give me error messages, I am not able to run the PreviewProvider in the second View. The main problem is in #main:
import SwiftUI
#main
struct ThisIsTheNameOfMyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.handlesExternalEvents(matching: ["main"])
WindowGroup("Scenarios") {
ScenarioView()
// error messages here: 'ScenarioView' initializer is inaccessible due to "private"
// protection level - I don't know what is set to private in ScenarioView that could
// cause this
// second error message: missing argument for parameter 'scenarios' in call
}
.handlesExternalEvents(matching: ["scenario"])
}
}
I am at a loss on how to solve these 2 error messages and would be very grateful for any tips or guidance. Apologies if this question is very long, I scanned many other forum questions and could not find any good answers.
I have tried adding pro forma data in #main as follows
#main
struct FloodModelScenarioViewerApp: App {
#State var scenarios = Scenario(scenarioNumber: 1, date: Date.now)
var body: some Scene {
WindowGroup {
ContentView()
}
.handlesExternalEvents(matching: ["main"])
WindowGroup("Scenarios") {
ScenarioView(scenarios: scenarios)
}
.handlesExternalEvents(matching: ["scenario"])
}
}
This still gives 2 error messages:
same issue with regards to ScenarioView initialiser being inaccessible due to being 'private'
Cannot convert value of type 'Scenario' to expected argument type 'OutputOverview'
Just remove the private from
#ObservedObject private var scenarios: OutputOverview
The value is coming from he parent so the parent needs access. So put
#StateObject private var scenarios: OutputOverview = .init()
in FloodModelScenarioViewerApp
#StateObject is for initializing ObservableObjects and #ObservedObject is for passing them around.
I don't know if your code will work after you read this question, and that's because there are many things to correct, but you can start with these:
In Cell, you shouldn't use an id that is a variable, this may cause inconsistent behavior. Use something like:
let id = UUID()
When you initialize ContentView, you can't use currentGrid inside a variable because currentGrid will not be available before all variables are initialized. Meaning, you are trying to initialize rows before currentGrid actually exists. You can try using the .onAppear modifier:
var rows = [GridItem]()
var body: some View {
GeometryReader { geometry in
// ... view code in here
}
.onAppear {
var rows = Array(repeating: GridItem(.flexible()), count: currentGrid.rowTotal)
}
}
This creates the view and, before showing it, the grid is set to its proper value.
The message 'ScenarioView' initializer is inaccessible due to "private" protection level seems clear: you must provide a value to to the variable scenarios (it doesn't have a default value) but it's marked as private. Remove private.
#ObservedObject var scenarios: OutputOverview
Then, remember to pass a value of type OutputOverview for the variable when you call the view:
ScenarioView(scenarios: aVariableOfTypeOutputOverview)
The type mismatch error you get inside the #main code is also clear - you have defined a variable of type Scenario:
#State var scenarios = Scenario(scenarioNumber: 1, date: Date.now)
but ScenarioView requires another type:
#ObservedObject private var scenarios: OutputOverview
One of them needs change for your code to work.

Configuring WKInterfaceTable row controller at load time

I'm doing most of my watch app UI in code and would like to configure row controller's IB properties at load time, before I access it with rowController(at:).
Is there an easy way to do it? All IB properties are nil at init(), and awakeFromNib() is not available for NSObject in WatchKit.
Your IBOutlets must be already bound to your custom table row class which is used in your Interface storyboard. In the custom row class you can update the interface controls by setting the object keeping the information to display.
class TableMainCellType: NSObject {
#IBOutlet weak var label:WKInterfaceLabel!
#IBOutlet weak var group:WKInterfaceGroup!
var item:MyTableItem? {
didSet{
label.setText(item?.title)
group.setBackgroundColor(item?.color)
}
}
}
To update the table cells I would iterate in the function to update the table view in the InterfaceController like below.
// ...
for index in tableItems.indices{
if let row = self.table.rowController(at: index) as? TableMainCellType {
let item = tableItems[index]
row.item = item
}
}
// ...

NSSlider object crashes code when it's moved on view

I've created an NSViewController with corresponding XIB in IB.
FWIW, I have the following window/view hierarchy:
class RxDemoController: NSWindowController {
#IBOutlet weak var statusPaneView: NSView!
#IBOutlet weak var annunciatorPaneView: NSView!
#IBOutlet weak var trigaStatusPaneView: NSView!
#IBOutlet weak var rxTabView: NSView!
override var windowNibName :String? { return "RxDemoController" }
override func windowDidLoad() {
super.windowDidLoad()
let statusPaneViewController = StatusPaneViewController()
statusPaneView.addSubview( statusPaneViewController.view )
let annunciatorPaneViewController = AnnunciatorPaneViewController()
annunciatorPaneView.addSubview( annunciatorPaneViewController.view )
let trigaStatusPaneController = TrigaStatusPaneController()
trigaStatusPaneView.addSubview( trigaStatusPaneController.view )
let rxTabController = RxTabController()
rxTabView.addSubview( rxTabController.view )
}
}
We can ignore the statesPaneViewController, annunciatorPaneViewController, and trigaStatusPaneController as they are all working properly. The rxTabController object is a tab view, defined in RXTabController.swift as follows:
class RxTabController: NSTabViewController {
#IBOutlet weak var rxDisplay1TabView: NSView!
override var nibName :String? { return "RxTabController" }
override func viewDidLoad() {
super.viewDidLoad()
let rxDisplay1ViewController = RxDisplay1()
rxDisplay1TabView.addSubview( rxDisplay1ViewController.view )
}
RxDisplay1.xib currently contains two objects: a bar graph object (custom component: LinearBarGraph_t which has been working fine in other projects and works fine on the view controller for the first tab), the second object is an NSSlider. The NSSlider is giving me problems. Here's the code for RxDisplay1:
class RxDisplay1: NSViewController {
#IBOutlet weak var linearPwrBG: LinearBarGraph_t!
override func viewDidLoad() {
super.viewDidLoad()
linearPwrBG.pctInRange = 50
}
#IBAction func linearPwrSlider(sender: NSSlider) {
}
}
I have right-dragged from the bar graph object to produce the #IBOutlet weak var linearPwrBG: LinearBarGraph_t! outlet; note that in viewDidLoad I set the pctInRange field to 50. This works fine when the app starts up (the bar graph is filled to the 50% mark).
The problem is with the slider. I've right dragged from the slider to the RxDisplay1 class to produce the linearPwrSlider action function. The connection seems to be fine (little filled dot next to the function declaration and right-clicking on the slider shows the action going to this function).
When I run the code, the display (and tab) properly appear. My bar graph shows 50% power. However, the moment I touch the slider with the mouse the system crashes. I get a break in AppDelegate complaining about "Bad Access".
I've used sliders all the time in past projects. However, this is the first time I've used a tabbed controller so I suspect there is something wrong with the way I've connected the tabbed controller to the rest of the system.

swift empty dictionary bug? playground vs project

this code work in playground
var detaildata:Dictionary=[:]
detaildata = ["apple":"hello"]
detaildata["orange"]="byebye"
this code don't work in project
class ViewController: UIViewController{
var detaildata:Dictionary=[:]
override func viewDidLoad() {
super.viewDidLoad()
detaildata = ["apple":"hello"]
detaildata["orange"]="byebye" // Error -> 'Dictionary' is not identical to 'Dictionary<key,Value>'
}
}
do you know why?
I think same code.
That code doesn't work in my playground, and shouldn't work in yours. When you declare a Dictionary, you need to give it both key and value types either explicitly or through type inference. All of these will work:
var dict1 = ["apple": "hello"] // inferred
var dict2: [String: String] = [:] // explicit
var dict3: Dictionary<String, String> = ["apple": "hello"] // longest version
Neither of these will work:
var dict3: Dictionary = [:] // type inference impossible
var dict4 = [:] // same

Adding observer for KVO without pointers using Swift

In Objective-C, I would normally use something like this:
static NSString *kViewTransformChanged = #"view transform changed";
// or
static const void *kViewTransformChanged = &kViewTransformChanged;
[clearContentView addObserver:self
forKeyPath:#"transform"
options:NSKeyValueObservingOptionNew
context:&kViewTransformChanged];
I have two overloaded methods to choose from to add an observer for KVO with the only difference being the context argument:
clearContentView.addObserver(observer: NSObject?, forKeyPath: String?, options: NSKeyValueObservingOptions, context: CMutableVoidPointer)
clearContentView.addObserver(observer: NSObject?, forKeyPath: String?, options: NSKeyValueObservingOptions, kvoContext: KVOContext)
With Swift not using pointers, I'm not sure how to dereference a pointer to use the first method.
If I create my own KVOContext constant for use with the second method, I wind up with it asking for this:
let test:KVOContext = KVOContext.fromVoidContext(context: CMutableVoidPointer)
EDIT: What is the difference between CMutableVoidPointer and KVOContext? Can someone give me an example how how to use them both and when I would use one over the other?
EDIT #2: A dev at Apple just posted this to the forums: KVOContext is going away; using a global reference as your context is the way to go right now.
There is now a technique officially recommended in the documentation, which is to create a private mutable variable and use its address as the context.
(Updated for Swift 3 on 2017-01-09)
// Set up non-zero-sized storage. We don't intend to mutate this variable,
// but it needs to be `var` so we can pass its address in as UnsafeMutablePointer.
private static var myContext = 0
// NOTE: `static` is not necessary if you want it to be a global variable
observee.addObserver(self, forKeyPath: …, options: [], context: &MyClass.myContext)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
if context == &myContext {
…
}
else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
Now that KVOContext is gone in Xcode 6 beta 3, you can do the following. Define a global (i.e. not a class property) like so:
let myContext = UnsafePointer<()>()
Add an observer:
observee.addObserver(observer, forKeyPath: …, options: nil, context: myContext)
In the observer:
override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: [NSObject : AnyObject]!, context: UnsafePointer<()>) {
if context == myContext {
…
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
Swift 4 - observing contentSize change on UITableViewController popover to fix incorrect size
I had been searching for an answer to change to a block based KVO because I was getting a swiftlint warning and it took me piecing quite a few different answers together to get to the right solution. Swiftlint warning:
Block Based KVO Violation: Prefer the new block based KVO API with keypaths when using Swift 3.2 or later. (block_based_kvo).
My use case was to present a popover controller attached to a button in a Nav bar in a view controller and then resize the popover once it's showing - otherwise it would be too big and not fitting the contents of the popover. The popover itself was a UITableViewController that contained static cells, and it was displayed via a Storyboard segue with style popover.
To setup the block based observer, you need the following code inside your popover UITableViewController:
// class level variable to store the statusObserver
private var statusObserver: NSKeyValueObservation?
// Create the observer inside viewWillAppear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
statusObserver = tableView.observe(\UITableView.contentSize,
changeHandler: { [ weak self ] (theTableView, _) in self?.popoverPresentationController?.presentedViewController.preferredContentSize = theTableView.contentSize
})
}
// Don't forget to remove the observer when the popover is dismissed.
override func viewDidDisappear(_ animated: Bool) {
if let observer = statusObserver {
observer.invalidate()
statusObserver = nil
}
super.viewDidDisappear(animated)
}
I didn't need the previous value when the observer was triggered, so left out the options: [.new, .old] when creating the observer.
Update for Swift 4
Context is not required for block-based observer function and existing #keyPath() syntax is replaced with smart keypath to achieve swift type safety.
class EventOvserverDemo {
var statusObserver:NSKeyValueObservation?
var objectToObserve:UIView?
func registerAddObserver() -> Void {
statusObserver = objectToObserve?.observe(\UIView.tag, options: [.new, .old], changeHandler: {[weak self] (player, change) in
if let tag = change.newValue {
// observed changed value and do the task here on change.
}
})
}
func unregisterObserver() -> Void {
if let sObserver = statusObserver {
sObserver.invalidate()
statusObserver = nil
}
}
}
Complete example using Swift:
//
// AppDelegate.swift
// Photos-MediaFramework-swift
//
// Created by Phurg on 11/11/16.
//
// Displays URLs for all photos in Photos Library
//
// #see http://stackoverflow.com/questions/30144547/programmatic-access-to-the-photos-library-on-mac-os-x-photokit-photos-framewo
//
import Cocoa
import MediaLibrary
// For KVO: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html#//apple_ref/doc/uid/TP40014216-CH7-ID12
private var mediaLibraryLoaded = 1
private var rootMediaGroupLoaded = 2
private var mediaObjectsLoaded = 3
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var window: NSWindow!
var mediaLibrary : MLMediaLibrary!
var allPhotosAlbum : MLMediaGroup!
func applicationDidFinishLaunching(_ aNotification: Notification) {
NSLog("applicationDidFinishLaunching:");
let options:[String:Any] = [
MLMediaLoadSourceTypesKey: MLMediaSourceType.image.rawValue, // Can't be Swift enum
MLMediaLoadIncludeSourcesKey: [MLMediaSourcePhotosIdentifier], // Array
]
self.mediaLibrary = MLMediaLibrary(options:options)
NSLog("applicationDidFinishLaunching: mediaLibrary=%#", self.mediaLibrary);
self.mediaLibrary.addObserver(self, forKeyPath:"mediaSources", options:[], context:&mediaLibraryLoaded)
NSLog("applicationDidFinishLaunching: added mediaSources observer");
// Force load
self.mediaLibrary.mediaSources?[MLMediaSourcePhotosIdentifier]
NSLog("applicationDidFinishLaunching: done");
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
NSLog("observeValue: keyPath=%#", keyPath!)
let mediaSource:MLMediaSource = self.mediaLibrary.mediaSources![MLMediaSourcePhotosIdentifier]!
if (context == &mediaLibraryLoaded) {
NSLog("observeValue: mediaLibraryLoaded")
mediaSource.addObserver(self, forKeyPath:"rootMediaGroup", options:[], context:&rootMediaGroupLoaded)
// Force load
mediaSource.rootMediaGroup
} else if (context == &rootMediaGroupLoaded) {
NSLog("observeValue: rootMediaGroupLoaded")
let albums:MLMediaGroup = mediaSource.mediaGroup(forIdentifier:"TopLevelAlbums")!
for album in albums.childGroups! {
let albumIdentifier:String = album.attributes["identifier"] as! String
if (albumIdentifier == "allPhotosAlbum") {
self.allPhotosAlbum = album
album.addObserver(self, forKeyPath:"mediaObjects", options:[], context:&mediaObjectsLoaded)
// Force load
album.mediaObjects
}
}
} else if (context == &mediaObjectsLoaded) {
NSLog("observeValue: mediaObjectsLoaded")
let mediaObjects:[MLMediaObject] = self.allPhotosAlbum.mediaObjects!
for mediaObject in mediaObjects {
let url:URL? = mediaObject.url
// URL does not extend NSObject, so can't be passed to NSLog; use string interpolation
NSLog("%#", "\(url)")
}
}
}
}

Resources