Posts

Post not yet marked as solved
13 Replies
11k Views
I added @EnvironmentObject to a property in a SwiftUI view as part of the official tutorial Adding User Interaction and added the .environmentObject modifier to the preview. However, I get the error:Cannot preview in this file - MyApp.app may have crashedThis error now appears in every SwiftUI project, even ones without EnvironmentObject or a blank SwiftUI template, so I cannot use previews. I have tried rebooting several times, and made sure the command line tools are set to Xcode 11. How can I fix this? Thanks!
Posted
by eos-pi.
Last updated
.
Post marked as solved
3 Replies
2.1k Views
I'm using WatchConnectivity and the WCSessionDelegate to successfully receive user info from my iOS app to my watchOS extension. When new data is received, I want to update my SwiftUI view with the latest information. I have an @ObservedObject property in the HostingController which is set to the latest data when it arrives. However, this does not trigger a UI update. How can I fix this? I've posted the code below. Thank you!import WatchKit import SwiftUI import WatchConnectivity import Combine class UserData: ObservableObject { @Published var account: Account = Account.getCurrentAccount() } class HostingController: WKHostingController<ContentView>, WCSessionDelegate { @ObservedObject private var userData: UserData = UserData() override init() { super.init() if WCSession.isSupported() { print("WCSession supported") let session = WCSession.default session.delegate = self session.activate() } } override var body: ContentView { return ContentView(userData: userData) } func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { } func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) { guard let accountJSON = userInfo["main-account"] as? String else { return } let userDefaults = UserDefaults() userDefaults.set(accountJSON, forKey: "main-account") self.userData.account = Account.getCurrentAccount() print("\n\nReceived this: \(accountJSON) \n\n") } }
Posted
by eos-pi.
Last updated
.
Post not yet marked as solved
5 Replies
1.7k Views
I have noticed lag and low frame rate (30-36fps) in ARKit apps built with Xcode 11.4 on the new iPad Pro (12.9" 4th gen). Exisitng AR apps from the App Store and AR QuickLook work as expected. The same apps built with Xcode 11.4 run well on my iPhone XR. For an example, I ran the RealityKit template app with no modifications on each device, and the iPad has anywhere from 30-36 fps fps while the iPhone stays constant at 60 fps. Is anyone else experiencing this issue? Thanks!
Posted
by eos-pi.
Last updated
.
Post not yet marked as solved
0 Replies
428 Views
I have a TabView with the PageTabViewStyle, and I am trying to implement continuous scrolling. After the last page, I'd like a right swipe to go back to the first page, and on the first page, a left swipe should go to the last page. I couldn't find a default option for this, so I attempted the following modifier. The gesture doesn't seem to work directly on top of the TabView, so is there a way to implement this? Thank you! TabView(selection: $currentIndex) { &#9;&#9;ForEach(0..<count) { item in &#9;&#9;&#9;&#9; Text("\(item)") &#9;&#9;} .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))             .gesture(                 DragGesture()                     .onEnded { value in                         if currentIndex == count-1 {                             currentIndex = 0                         }                     }             )
Posted
by eos-pi.
Last updated
.
Post marked as solved
3 Replies
910 Views
In Xcode 9 Beta 4, any new segues I create with a destination View Controller that has a custom class show a black screen that covers all contents. If I remove the destination View Controller's custom class, the segue works as expected. Is anyone else experiencing this issue?
Posted
by eos-pi.
Last updated
.
Post marked as solved
4 Replies
1.7k Views
I am building an app which involves saving schedules and displaying them in a table view. I am using NSCoding to save an array of custom Schedule objects. However, when I reload the app after entering a schedule, the table view is empty again. NSKeyedArchiver and NSKeyedUnarchiver both run successfully, but I am only getting an empty array after restarting the app. What am I doing wrong?Here is the Schedule class:import Foundation class Schedule: NSObject, NSCoding { var name: String var division: Bool var carrierA: String var carrierB: String var carrierC: String var carrierD: String var carrierE: String var carrierF: String var carrierG: String override var description: String { return "\(name)'s schedule is: \(carrierA), \(carrierB), \(carrierC), \(carrierD), \(carrierE), \(carrierF), \(carrierG)" } struct PropertyKey { static let name = "name" static let division = "division" static let carrierA = "carrierA" static let carrierB = "carrierB" static let carrierC = "carrierC" static let carrierD = "carrierD" static let carrierE = "carrierE" static let carrierF = "carrierF" static let carrierG = "carrierG" } static let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! static let archiveURL = documentsDirectory.appendingPathComponent("schedules") init(name: String, division: Bool, carrierA: String, carrierB: String, carrierC: String, carrierD: String, carrierE: String, carrierF: String, carrierG: String) { self.name = name self.division = division self.carrierA = carrierA self.carrierB = carrierB self.carrierC = carrierC self.carrierD = carrierD self.carrierE = carrierE self.carrierF = carrierF self.carrierG = carrierG } static func loadFromFile() -&gt; [Schedule]? { return NSKeyedUnarchiver.unarchiveObject(withFile: Schedule.archiveURL.path) as? [Schedule] } static func saveToFile(schedules: [Schedule]) { NSKeyedArchiver.archiveRootObject(schedules, toFile: Schedule.archiveURL.path) } required convenience init?(coder aDecoder: NSCoder) { guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String, let division = aDecoder.decodeObject(forKey: PropertyKey.division) as? Bool, let carrierA = aDecoder.decodeObject(forKey: PropertyKey.carrierA) as? String, let carrierB = aDecoder.decodeObject(forKey: PropertyKey.carrierB) as? String, let carrierC = aDecoder.decodeObject(forKey: PropertyKey.carrierC) as? String, let carrierD = aDecoder.decodeObject(forKey: PropertyKey.carrierD) as? String, let carrierE = aDecoder.decodeObject(forKey: PropertyKey.carrierE) as? String, let carrierF = aDecoder.decodeObject(forKey: PropertyKey.carrierF) as? String, let carrierG = aDecoder.decodeObject(forKey: PropertyKey.carrierG) as? String else { return nil } self.init(name: name, division: division, carrierA: carrierA, carrierB: carrierB, carrierC: carrierC, carrierD: carrierD, carrierE: carrierE, carrierF: carrierF, carrierG: carrierG) } func encode(with aCoder: NSCoder) { aCoder.encode(name, forKey: PropertyKey.name) aCoder.encode(division, forKey: PropertyKey.division) aCoder.encode(carrierA, forKey: PropertyKey.carrierA) aCoder.encode(carrierB, forKey: PropertyKey.carrierB) aCoder.encode(carrierC, forKey: PropertyKey.carrierC) aCoder.encode(carrierD, forKey: PropertyKey.carrierD) aCoder.encode(carrierE, forKey: PropertyKey.carrierE) aCoder.encode(carrierF, forKey: PropertyKey.carrierF) aCoder.encode(carrierG, forKey: PropertyKey.carrierG) } }And here is the TableViewController:import UIKit class SettingsTableViewController: UITableViewController { var schedules = [Schedule]() var previousIndex: Int? @objc func refreshControlActivated(sender: UIRefreshControl) { tableView.reloadData() sender.endRefreshing() } / override func viewDidLoad() { super.viewDidLoad() / / self.navigationItem.title = "Manage Schedules" if #available(iOS 11, *) { navigationController?.navigationBar.prefersLargeTitles = true } if let savedSchedules = Schedule.loadFromFile() { print(savedSchedules) schedules = savedSchedules tableView.reloadData() print("Fetched fresh, new schedules!") print(schedules) } self.refreshControl = UIRefreshControl() self.refreshControl?.addTarget(self, action: #selector(refreshControlActivated(sender:)), for: .valueChanged) / self.clearsSelectionOnViewWillAppear = false / / } override func viewWillAppear(_ animated: Bool) { tableView.reloadData() / } override func viewWillDisappear(_ animated: Bool) { Schedule.saveToFile(schedules: schedules) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() / } / override func numberOfSections(in tableView: UITableView) -&gt; Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int { return schedules.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) / let schedule = schedules[indexPath.row] cell.textLabel?.text = schedule.name return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { previousIndex = indexPath.row } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { / / guard let formViewController = segue.destination as? FormViewController else { return } if let indexPath = tableView.indexPathForSelectedRow, segue.identifier == "editSchedule" { formViewController.schedule = self.schedules[indexPath.row] } } @IBAction func unwindToTable(segue: UIStoryboardSegue) { guard let source = segue.source as? FormViewController, let schedule = source.schedule else { return } if let indexPath = tableView.indexPathForSelectedRow { schedules.remove(at: indexPath.row) schedules.insert(schedule, at: indexPath.row) schedules.remove(at: indexPath.row) tableView.deselectRow(at: indexPath, animated: true) tableView.reloadData() } else { schedules.append(schedule) } Schedule.saveToFile(schedules: schedules) } }
Posted
by eos-pi.
Last updated
.