Simplify Swift data struct - swift

Any suggestion on how to simplify this data struct? The data will be saved as a dictionary on the user's drive and when I read the data from the drive I have to convert them back to Member for easy accessing the properties.
I would like to have it typesafe.
struct Member {
var id: Int
var firstname: String
var lastname: String
var address: String?
var zipCode: Int?
var city: String?
enum Value: String {
case id = "id"
case firstname = "firstname"
case lastname = "lastname"
case address = "address"
case zipCode = "zipCode"
case city = "city"
}
var member: [String:Any] {
return [
Value.id.rawValue:Int(),
Value.firstname.rawValue:firstname,
Value.lastname.rawValue:lastname,
Value.address.rawValue:address ?? String(),
Value.zipCode.rawValue:zipCode ?? Int(),
Value.city.rawValue:city ?? String()
]
}
}
func memberToDic(member: Member) -> [String:Any] {
return [
Member.Value.firstname.rawValue:member.firstname,
Member.Value.lastname.rawValue:member.lastname,
Member.Value.address.rawValue:member.address ?? String(),
Member.Value.zipCode.rawValue:member.zipCode ?? Int(),
Member.Value.city.rawValue:member.city ?? String()
]
}
func dicToMember(dic: [String:Any]) -> Member {
return Member(
id: dic[Member.Value.id.rawValue] as! Int,
firstname: dic[Member.Value.firstname.rawValue] as! String,
lastname: dic[Member.Value.lastname.rawValue] as! String,
address: dic[Member.Value.address.rawValue] as? String,
zipCode: dic[Member.Value.zipCode.rawValue] as? Int,
city: dic[Member.Value.city.rawValue] as? String
)
}

Almost certainly, this is the correct implementation:
struct Member: Codable {
var id: Int
var firstName: String // "first name" is two words, so capitalize "name"
var lastName: String
var address: String // "No address" should be empty, not nil
var zipCode: String // ZIP codes are not integers
var city: String // "No city" should be empty, not nil
}
In order to save this as a plist, use PropertyListEncoder:
let data = try PropertyListEncoder().encode(member)
To read it, use PropertyListDecoder.
Codable automatically creates key mappings for your properties, so there's no need for Value.
You should strongly avoid creating or consuming [String: Any] dictionaries. These exist mostly due to Objective-C interfaces that could not generate strong types.
If address, zipCode, and city all should be set together, or not set together, then you should collect them into a single struct:
struct Address: Codable {
var streetAddress: String
var zipCode: String
var city: String
}
struct Member: Codable {
var id: Int
var firstName: String // "first name" is two words, so capitalize "name"
var lastName: String
var address: Address?
}
In this case, and Optional makes sense because "empty" is not the same thing as "missing."

Related

Type of expression is ambiguous class error

I am fairly new to the swift language I am just taking a course I bought and I found myself running into the same error code a lot. "Type is ambiguous without more context" and I understand what it means (i think), SO. I am trying to assign a variable that has a class stored inside to another parameter of a class if that makes any sense. I understand that I'm trying to assign a class to a string, but I guess my question is how can I make this work? is there a type I don't know about in swift that would allow me to do such a thing?
class Adress {
let street: String
let city: String
let postalCode: String
init(street: String, city: String, postalCode: String) {
self.street = street
self.city = city
self.postalCode = postalCode
}
}
class PurchaseOrder {
var id: Int
var shippingAdress: (String)
var billingAdress: (String)
init(id: Int, shippingAdress: String, billingAdress:String) {
self.id = id
self.shippingAdress = shippingAdress
self.billingAdress = billingAdress
}
}
var defaultAddress = Adress(street: "555 North Pole Ave.", city: "North Pole City", postalCode: "H0H 0H0")
var aliceGift = PurchaseOrder(id: 001, shippingAdress: defaultAddress, billingAdress: defaultAddress)
// THE ERROR IS HERE ^^^^^^.
aliceGift.shippingAdress = "1000 North Pole Blvd."
print(aliceGift.billingAdress)
print(aliceGift.shippingAdress)
You have a custom Adress class so you should use it in your code. Change PurchaseOrder to
class PurchaseOrder {
var id: Int
var shippingAdress: Adress
var billingAdress: Adress
init(id: Int, shippingAdress: Adress, billingAdress:Adress) {
self.id = id
self.shippingAdress = shippingAdress
self.billingAdress = billingAdress
}
}
Of course you need to change the rest of the code as well so you assign an Adress object and not a String if you want to change any adress property
You've declared the types of shippingAddress and billingAddress incorrectly, as String instead of Address.
class Address {
let street: String
let city: String
let postalCode: String
init(street: String, city: String, postalCode: String) {
self.street = street
self.city = city
self.postalCode = postalCode
}
}
class PurchaseOrder {
var id: Int
var shippingAddress: Address
var billingAddress: Address
init(id: Int, shippingAddress: Address, billingAddress: Address) {
self.id = id
self.shippingAddress = shippingAddress
self.billingAddress = billingAddress
}
}
var defaultAddress = Address(street: "555 North Pole Ave.", city: "North Pole City", postalCode: "H0H 0H0")
var aliceGift = PurchaseOrder(id: 001, shippingAddress: defaultAddress, billingAddress: defaultAddress)

Using models with sub models in CoreData

I am trying to implement a cache using CoreData.
Up until this point I've been storing models that are simple, however I have a model below that contains data types such as CodablePartialUser and CodableFeedItemType.
How should these types be modelled in CoreData?
Should I use the Data type and store them in a data format?
As CodableFeedItemType is an enum, should I store the raw value and convert between formats again?
struct CodablePartialUser: Equatable, Codable {
let userID: String
let firstName: String
let lastName: String
init(userID: String, firstName: String, lastName: String) {
self.userID = userID
self.firstName = firstName
self.lastName = lastName
}
}
enum CodableFeedItemType: String, Codable {
case recognition = "RECOGNITION"
case news = "COMPANY_NEWS"
}
struct CodableFeedItem: Codable {
let id: String
let type: CodableFeedItemType
let createdDate: Date
let createdBy: CodablePartialUser
let likesCount: Int
let commentsCount: Int
let externalID: String
let title: String?
let imageURL: URL?
init(id: String, type: CodableFeedItemType, createdDate: Date, createdBy: CodablePartialUser, likesCount: Int, commentsCount: Int, externalID: String, title: String?, imageURL: URL?) {
self.id = id
self.type = type
self.createdDate = createdDate
self.createdBy = createdBy
self.likesCount = likesCount
self.commentsCount = commentsCount
self.externalID = externalID
self.title = title
self.imageURL = imageURL
}
}
For the CodablePartialUser you can use relationship by creating a new Entity named as "CodablePartialUser"
For CodableFeedItemType you can use enum as like
enum CodableFeedItemType: String, Codable {
case recognition = "RECOGNITION"
case news = "COMPANY_NEWS"
}
extension CodableFeedItemEntity {
var type: CodableFeedItemType {
get {
return CodableFeedItemType(rawValue: typeRaw)!
}
set {
typeRaw = newValue.rawValue
}
}
}

Copy one struct object

i have to quite similar struct objects. but one includes more values than the other. As the initial is required for KituraKuery methods i can not modify it but require more information for future processing.
my problem is now, that these struct objects look like this:
struct provider: Codable {
var firstName: String?
var lastName: String?
var email:String?
}
extension provider: Model{
class Persistence {}
}
struct provider2: Codable {
var firstName: String?
var lastName: String?
var email:String?
var providerCategories: [categories]?
}
extension provider: Model{
class Persistence {}
}
what i need is basically a smarter way to copy information from provider to provider2.
what i did as of now is i provided an init to provider2 taking provider as input and adding all values to it.
struct provider2: Codable {
var firstName: String?
var lastName: String?
var email:String?
var providerCategories: [categories]?
init(provider: provider?) {
if let provider = provider{
firstName = provider.firstName
lastName = provider.lastName
email = provider.lastName
}
}
extension provider: Model{
class Persistence {}
}
i however believe this is probably the worst way and there are much better and more lean approaches to it.
I tried myself on protocols but could that not really get to work.
Any input would be great :)
In your approach both Provider and Provider2 struct are tightly coupled to each other. So lets say in future if you want to change Provider struct or you want to initialise Provider2 struct with another struct, then you need to change lot of things.
We can solve the problem easily by decoupling both Provider and Provider2 struct
protocol BasicInfo {
var firstName: String? { get set }
var lastName: String? { get set }
var email:String? { get set }
}
protocol Address {
var address: String? {get set}
}
struct Provider: BasicInfo {
var firstName: String?
var lastName: String?
var email: String?
}
struct Provider2: BasicInfo, Address {
var firstName: String?
var lastName: String?
var email:String?
var address: String?
init(basic: BasicInfo, add: String) {
firstName = basic.firstName
lastName = basic.lastName
email = basic.email
address = add
}
}
//Below are instances of respective struct
let provider1 = Provider(firstName: "Test1", lastName: "TestLast1", email: "test1#gmail.com")
var provider2 = Provider2(basic: provider1, add: "Germany")
In above code i have two different Struct Provider and Provider2. Provider2 contains more variable than Provider (i have just added a single var to demonstrate). Now lets say in future we don't require Provider to fill Provider2, we have a new struct Provider3 which will fill Provider2 values.
struct Provider3: BasicInfo {
var firstName: String?
var lastName: String?
var email: String?
var middleName: String? //new property added
}
//Below are instances of respective struct
let provider3 = Provider3(firstName: "Test1", lastName: "TestLast1", email: "test1#gmail.com")
var provider2 = Provider2(basic: provider3, add: "Germany")
As you see there is no changes in struct Provider2, we have just introduce a new struct, create instance of new struct and passed that instance to Provider2 init method.
You could use extensions for this:
extension provider {
func asProvider2() -> provider2 {
return provider2(firstName: firstName,
lastName: lastName,
email: email,
providerCategories: nil)
}
}
// Then you can use it like this:
let bar = provider(firstName: "foo", lastName: "bar", email: "baz")
let bar2 = bar.asProvider2()

How can I init a struct from the values of a different struct

I have a user profile I am storing with a struct shaped like
struct Profile: Codable {
let company: String?
let country: String?
let createdDate: String?
let dateOfBirth: String?
let department: String?
let email: String?
let employeeKey: String?
let firstName: String?
let gender: String?
let id: String?
let jobTitle: String?
let lastName: String?
let location: String?
let mobileDeviceToken: String?
let pictureUri: String?
let roles: [String]?
let status: String?
let updatedDate: String?
let userId: String?
let webDeviceToken: String?
let webMobileDeviceToken: String?
enum CodingKeys: String, CodingKey {
case company = "company"
case country = "country"
case createdDate = "createdDate"
case dateOfBirth = "dateOfBirth"
case department = "department"
case email = "email"
case employeeKey = "employeeKey"
case firstName = "firstName"
case gender = "gender"
case id = "id"
case jobTitle = "jobTitle"
case lastName = "lastName"
case location = "location"
case mobileDeviceToken = "mobileDeviceToken"
case pictureUri = "pictureUri"
case roles = "roles"
case status = "status"
case updatedDate = "updatedDate"
case userId = "userId"
case webDeviceToken = "webDeviceToken"
case webMobileDeviceToken = "webMobileDeviceToken"
}
}
I have another struct which looks like
struct ArticleAuthor {
let name: String
let department: String
let email: String
}
When fetching a user profile, I'd like to be able to create my ArticleAuthor struct using the Profile object returned from my Profile Service.
I was hoping to do something like this, but it does not work as the from value is expected to be data.
self?.profileService.fetchForUserByUserId(userId: authorId) { [weak self] profile, error in
guard error == nil else { return }
let author = try? JSONDecoder().decode(ArticleAuthor.self, from: profile)
print(author) // should be a populated author property
}
I was hoping to avoid something like let author = ArticleAuthor(name: profile?.firstName, department: profile?.department, email: profile?.email) as this object could grow in time.
The profile object in your sample code is already 'Decoded', so you dont need to decode it again.
To avoid using the default init, you can just add a custom initializer, so that you can pass in a Profile struct and set the values. This is usually the best way to go about it as it prevents making lots of changes throughout the codebase when you add new properties
struct ArticleAuthor {
let name: String?
let department: String?
let email: String?
init(profile: Profile) {
self.name = profile.firstName
self.department = profile.department
self.email = profile.email
}
}
self?.profileService.fetchForUserByUserId(userId: authorId) { [weak self] profile, error in
guard error == nil else { return }
let author = Author(profile: profile)
print(author) // should be a populated author property
}

Swift: Reducing the length of Init methods

I would like to reduce the length of init method.
struct Person {
var id: Int
var firstName: String
var lastName: String
var vehicle: String
var location: String
var timeZone: String
init (id: Int, firstName: String, lastName: String, vehicle: String, location: String, timeZone: String ) {
self.firstName = firstName
self.lastName = lastName
self.vehicle = vehicle
self.location = location
self.timeZone = timeZone
}
}
Below is an instance of Person I am creating. I have to pass in the value of every single variable inline.
let person = Person(id: 22, firstName: "John", lastName: "Doe", vehicle: "Chevy", location: "Dallas", timeZone: "CST")
Question: How can I shrink the length of init? In Obj-C I used to create a data model class. Populate it's variables and then pass the entire class, reducing the length of the init method.
i.e.
Person *person = [Person new];
person.id = 22;
person.firstName = "John";
person.lastName = "Doe";
person.vehicle = "Chevy";
person.location = "Dallas";
person.timeZone = "CST"
Person *person = [Person initWithPerson:person];
What's an equivalent way in Swift to reduce the length of init without having to initialize every single variable inline? I know tuples is one way, is there any other best practice?
Just remove the initializer!
struct Person {
let id: Int
let firstName: String
let lastName: String
let vehicle: String
let location: String
let timeZone: String
}
Now you can use the memberwise initializer
Person(
id: 87112,
firstName: "Walter",
lastName: "White",
vehicle: "2004 Pontiac Aztek",
location: "Albuquerque",
timeZone: "UTC-07:00"
)
Structure types automatically receive a memberwise initializer if they do not define any of their own custom initialisers.
The Swift Programming Language
DO NOT use var
As you can see I replaced var with let.
Unless you need to change some properties of a Person after the value has been created, I suggest you to use let. Otherwise you are free to use var. This way the compiler will prevent unwanted changes.
DO NOT use Optionals
I don't know the business logic of your app, however if a Person must have all that 6 properties always populated, don't make them optionals. Otherwise every time you need to use a Person value the compiler will force you to check if that optional has a value.
DO NOT use Implicitly Unwrapped Optionals
Seriously. There are a few cases where they are useful and a model value is not one of them
Using a struct you actually don't need an initializer
struct Person {
var id : Int?
var firstName: String?
var lastName: String?
var vehicle: String?
var location: String?
var timeZone: String?
}
var person = Person()
person.id = 22
person.firstName = "John"
person.lastName = "Doe"
person.vehicle = "Chevy"
person.location = "Dallas"
person.timeZone = "CST"
You can do the same with non-optionals
struct Person {
var id = 0
var firstName = ""
var lastName = ""
var vehicle = ""
var location = ""
var timeZone = ""
}
Consider also the benefit of an initializer to declare (read-only) constants
struct Person {
let id : Int
let firstName : String
let lastName : String
let vehicle : String
let location : String
let timeZone : String
}
In this case you have to use the implicit memberwise initializer.
let person = Person(id: 22, firstName: "John", lastName: "Doe", vehicle: "Chevy", location: "Dallas", timeZone: "CST")
Like it was mentioned in the comments, an initializer will be created for you, and it'll look like this:
Person(id: Int?, firstName: String?, lastName: String?, vehicle: String?, location: String?, timeZone: String?)
However, you can also do this:
var person = Person()
person.id = 100
person.firstName = "Name"
...
Like you used to do in ObjC. Note that person was declared as var, because if it was declared as let, you wouldn't be able to mutate it.