From 6c40ef4e4028273d390bde8efc2b672c695274a4 Mon Sep 17 00:00:00 2001 From: Mina Her Date: Fri, 27 May 2022 22:38:56 +0900 Subject: [PATCH] Shorten method body --- .../PersistentContainer+Preview.swift | 86 ++++++------------- 1 file changed, 24 insertions(+), 62 deletions(-) diff --git a/TweetNest/Shared/Previews/Extensions/TweetNestKit/PersistentContainer+Preview.swift b/TweetNest/Shared/Previews/Extensions/TweetNestKit/PersistentContainer+Preview.swift index 7894e4af..2a251b42 100644 --- a/TweetNest/Shared/Previews/Extensions/TweetNestKit/PersistentContainer+Preview.swift +++ b/TweetNest/Shared/Previews/Extensions/TweetNestKit/PersistentContainer+Preview.swift @@ -232,69 +232,31 @@ extension PersistentContainer { userDetail.userID = latestUserDetail.userID userDetail.username = latestUserDetail.username } - if let blockingUserIDs = previewUserDetail.blockingUserIDs { - userDetail.blockingUserIDs = blockingUserIDs - } - if let creationDate = previewUserDetail.creationDate { - userDetail.creationDate = creationDate - } - else { - userDetail.creationDate = .now - } - if let followerUserIDs = previewUserDetail.followerUserIDs { - userDetail.followerUserIDs = followerUserIDs - } - if let followerUsersCount = previewUserDetail.followerUsersCount { - userDetail.followerUsersCount = .init(followerUsersCount) - } - if let followingUserIDs = previewUserDetail.followingUserIDs { - userDetail.followingUserIDs = followingUserIDs - } - if let followingUsersCount = previewUserDetail.followingUsersCount { - userDetail.followingUsersCount = .init(followingUsersCount) - } - if let isProtected = previewUserDetail.isProtected { - userDetail.isProtected = isProtected - } - if let isVerified = previewUserDetail.isVerified { - userDetail.isVerified = isVerified - } - if let listedCount = previewUserDetail.listedCount { - userDetail.listedCount = .init(listedCount) - } - if let location = previewUserDetail.location { - userDetail.location = location - } - if let mutingUserIDs = previewUserDetail.mutingUserIDs { - userDetail.mutingUserIDs = mutingUserIDs - } - if let name = previewUserDetail.name { - userDetail.name = name - } - if let profileHeaderImageURL = previewUserDetail.profileHeaderImageURL { - userDetail.profileHeaderImageURL = profileHeaderImageURL - } - if let profileImageURL = previewUserDetail.profileImageURL { - userDetail.profileImageURL = profileImageURL - } - if let tweetsCount = previewUserDetail.tweetsCount { - userDetail.tweetsCount = .init(tweetsCount) - } - if let url = previewUserDetail.url { - userDetail.url = url - } - if let userAttributedDescription = previewUserDetail.userAttributedDescription { - userDetail.userAttributedDescription = .init(userAttributedDescription) - } - if let userCreationDate = previewUserDetail.userCreationDate { - userDetail.userCreationDate = userCreationDate - } - if let userID = previewUserDetail.userID { - userDetail.userID = userID - } - if let username = previewUserDetail.username { - userDetail.username = username + func assign(_ value: T?, to dest: inout T) { + if let value = value { + dest = value + } } + assign(previewUserDetail.blockingUserIDs, to: &userDetail.blockingUserIDs) + assign(previewUserDetail.creationDate ?? .now, to: &userDetail.creationDate) + assign(previewUserDetail.followerUserIDs, to: &userDetail.followerUserIDs) + assign(previewUserDetail.followerUsersCount.flatMap({.init($0)}), to: &userDetail.followerUsersCount) + assign(previewUserDetail.followingUserIDs, to: &userDetail.followingUserIDs) + assign(previewUserDetail.followingUsersCount.flatMap({.init($0)}), to: &userDetail.followingUsersCount) + assign(previewUserDetail.isProtected, to: &userDetail.isProtected) + assign(previewUserDetail.isVerified, to: &userDetail.isVerified) + assign(previewUserDetail.listedCount.flatMap({.init($0)}), to: &userDetail.listedCount) + assign(previewUserDetail.location, to: &userDetail.location) + assign(previewUserDetail.mutingUserIDs, to: &userDetail.mutingUserIDs) + assign(previewUserDetail.name, to: &userDetail.name) + assign(previewUserDetail.profileHeaderImageURL, to: &userDetail.profileHeaderImageURL) + assign(previewUserDetail.profileImageURL, to: &userDetail.profileImageURL) + assign(previewUserDetail.tweetsCount.flatMap({.init($0)}), to: &userDetail.tweetsCount) + assign(previewUserDetail.url, to: &userDetail.url) + assign(previewUserDetail.userAttributedDescription.flatMap({.init($0)}), to: &userDetail.userAttributedDescription) + assign(previewUserDetail.userCreationDate, to: &userDetail.userCreationDate) + assign(previewUserDetail.userID, to: &userDetail.userID) + assign(previewUserDetail.username, to: &userDetail.username) if save { do { try context.save()