Post

Replies

Boosts

Views

Activity

Reply to How to filter data from an api, and how to use userdefaults to load and save data?
I would recommend using Core Data instead of UserDefaults, since UserDefaults is designed for smaller scale things like device settings for an app for example. Core Data can easily handle the movie metadata. As for the thumbnail, you can store that too but its a little more complicated. I'm not entirely sure how to do it off the top of my head, but there should be a way to store it. In terms of integrating Core Data with your user interface, you can use the NSFetchedResultsController to connect your UI where you would use the values from the fetched results controller to return as part of the UIKit implementation. I would also recommend that you use Swift's new concurrency support if you're project has an iOS/iPadOS development target of iOS/iPadOS 15.0 or later. It'll make it more difficult to make mistakes such as leaving the caller of your methods hanging by not calling the completion handler by accident.
Topic: UI Frameworks SubTopic: UIKit Tags:
Dec ’21
Reply to "Sorry, you can’t enroll at this time." when enrolling.
You have to be at least 18 years old to enrol in the Apple Developer Program yourself. There however, I believe, exists a way for you to enrol otherwise. If I am not mistaken, you can enrol using an Apple ID of someone who is over the age of 18 (parent/guardian, for example) and use their account to access the services that the Apple Developer Program provides. I would recommend double checking with Apple Developer Support before continuing.
Jan ’22
Reply to Using an @EnvironmentObject Breaks Xcode Previews
I would recommend trying to put the .environmentObject modifier on the view that you're previewing or the top level group if there are two or more of them. In the modifier you'll have to specify an instance of your environment object. Based on your code sample, you could probably just put SettingsData() in the argument for the modifier to satisfy it. Here's an example of the preview code that might fit your needs (one view) static var previews: some View { SomeView() .environmentObject(SettingsData()) } or more than one view static var previews: some View { Group { SomeView() AnotherView() // Any other views that would be placed here } .environmentObject(SettingsData()) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
Reply to SwiftUI modifying state warning - pointing to @main
Here's my custom StoredRoutes type: import Foundation final class StoredRoutes: NSObject, NSSecureCoding {     static var supportsSecureCoding: Bool = true     func encode(with coder: NSCoder) {         var routeids: [NSString] = []         for route in routes {             routeids.append(NSString(string: route.rawValue))         }         coder.encode(NSArray(array: routeids), forKey: "routes")     }     init?(coder: NSCoder) {         if let nsArray = coder.decodeObject(of: NSArray.self, forKey: "routes") {             self.routes = []             for id in nsArray {                 self.routes.append(Route(rawValue: id as! String)!)             }         } else {             return nil         }     }     var routes: [Route]     required init(routes: [Route]) {         self.routes = routes     } } and my value transformer: import Foundation final class StoredRoutesValueTransformer: NSSecureUnarchiveFromDataTransformer {          static var name = NSValueTransformerName(rawValue: "StoredRoutesValueTransformer")          override class func allowsReverseTransformation() -> Bool {         return true     }          override class func transformedValueClass() -> AnyClass {         return StoredRoutes.self     }          override class var allowedTopLevelClasses: [AnyClass] {         return [StoredRoutes.self, NSArray.self, NSString.self]     }          override func transformedValue(_ value: Any?) -> Any? {         guard let data = value as? Data else {             fatalError("Wrong data type: value must be of type Data. The type the value recieved was \(type(of: value)).")         }         return super.transformedValue(data)     }          override func reverseTransformedValue(_ value: Any?) -> Any? {         guard let storedRoutes = value as? StoredRoutes else {             fatalError("Wrong data type: value must be of type StoredRoutes. The type of the value recieved was \(type(of: value))")         }         return super.reverseTransformedValue(storedRoutes)     }     public static func register() {         let transformer = StoredRoutesValueTransformer()         ValueTransformer.setValueTransformer(transformer, forName: name)     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
Reply to Using Xcode with arbitrary Git repository
You can set up a repository using another provider by ticking the "Create Git repository on my Mac" option and then in the source control navigator (panel on left side, second tab), expand the category for the repository. Right click on "Remotes" and select "Add Existing Remote...". In the prompt that appears in the centre of the screen, you should be able to put in your server information and add the remote. If you already have a remote configured and everything, Xcode should be able to just see the git repository and recognise the existence of the remotes, so pushing, pulling, committing and other simpler git operations should be available from the source control menu.
Jan ’23
Reply to Core Data - Warns of unarchival of a type that is included in the permitted classes for the associated value transformer
Update: This error occurred since the custom class StoredRoutes was trying to decode an NSArray, use the methods for decoding an array instead. Older code: public init?(coder: NSCoder) {         if let nsArray = coder.decodeObject(of: NSArray.self, forKey: "routes") {             self.routes = []             for id in nsArray {                 self.routes.append(Route(rawValue: id as! String)!)             }         } else {             return nil         }     } New code that fixes this issue: public init?(coder: NSCoder) {         if let nsArray = coder.decodeArrayOfObjects(ofClass: NSString.self, forKey: "routes") {             self.routes = []             for id in nsArray {                 self.routes.append(Route(rawValue: id as String)!)             }         } else {             return nil         }     }
Jan ’23
Reply to Having difficulty connecting relationships during a data import
The issues were occurring due to bad multithreading: Make sure that you are performing the code that you intend on the correct queue. static public func refreshData(for context: NSManagedObjectContext) async throws {         let relationships = try await StationFetcher().fetch(ignoresLowDataMode: false)         var stops = relationships         var stations = relationships                  try await context.perform {             let stopsRequest = NSBatchInsertRequest(entity: Stop.entity(), managedObjectHandler: { managedObject in                 guard !stops.isEmpty else {                     return true                 }                 let stop = managedObject as! Stop                 stop.update(from: stops.removeFirst())                 return false             })                          let stationsRequest = NSBatchInsertRequest(entity: Station.entity(), managedObjectHandler: { managedObject in                 guard !stations.isEmpty else {                     return true                 }                 let station = managedObject as! Station                 station.update(using: stations.removeFirst())                 return false             })                          guard (try context.execute(stopsRequest) as! NSBatchInsertResult).result as! Bool else {                 throw StationInitializationError.unsuccessfulInsertion             }                          guard (try context.execute(stationsRequest) as! NSBatchInsertResult).result as! Bool else {                 throw StationInitializationError.unsuccessfulInsertion             }                          context.refreshAllObjects()                          for stop in relationships {                 let stationRequest: NSFetchRequest<Station> = Station.fetchRequest()                 stationRequest.predicate = NSPredicate(format: "id = %@", argumentArray: [stop.stationid])                 let stopRequest: NSFetchRequest<Stop> = Stop.fetchRequest()                 stopRequest.predicate = NSPredicate(format: "id = %@", argumentArray: [stop.stopid])                                  let stationResults = try context.fetch(stationRequest)                 let stopResults = try context.fetch(stopRequest)                                  stationResults.first!.addToStops(stopResults.first!)             }                          if context.hasChanges {                 try context.save()             }         }     }
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’23
Reply to Older Versions of XCode
I'm sorry, however Apple are very insistent on using the latest version of macOS. The latest version of Xcode only works on macOS 13.x I think and the betas for the upcoming release of Xcode work only with macOS 13.4 and later. I would recommend updating your computer to the latest version of macOS. However, if it is not possible to do that due to lack of support (older computers typically loose support for the latest version of macOS after a number of years), I would recommend upgrading to a newer Mac that supports your needs.
Aug ’23