Creating an Enum with a Custom Type in Swift 4.x












0














I was in a code review today and someone said, "Why don't you do an enum on that?" I had started to, but conforming to a bunch of protocols seemed like a gigantic PITA and it seemed more error prone than just writing something simple, readable, and maintainable.



That said, I started to fiddle around with it this evening and I can't figure it out.



This is what I've started with:



typealias PolicyType = (filename: String, text: String)

struct Policy {
static let first = PolicyType(filename: "firstFile.txt", text: "text in first file")
static let second = PolicyType(filename: "secondFile.txt", text: "text in second file")
static let third = PolicyType(filename: "thirdFile.txt", text: "text in third file")
}

let thirdPolicyText = Policy.third.text


Is there a more memory efficient, maintainable way to do this with an enum? My primary objective is maintainability.



Below is what I've come up with. It feels hacky and looks like...well, you know:



public struct PolicyType : Equatable, ExpressibleByStringLiteral {
var filename: String
var text: String

//MARK:- Equatable Methods
public static func == (lhs: PolicyType, rhs: PolicyType) -> Bool {
return (lhs.filename == rhs.filename && lhs.text == rhs.text)
}

//MARK:- ExpressibleByStringLiteral Methods
public init(stringLiteral value: String) {
let components = value.components(separatedBy: "|")
self.filename = components[0]
self.text = components[1]
}

public init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
public init(extendedGraphemeClusterLiteral value: String) {
self.init(stringLiteral: value)
}
}

enum Policy: PolicyType {
case first = "testFile1.txt|This is sample text"
case second = "testFile2.txt|This is more sample text"
case third = "testFile3.txt|This is more sample text"
}

Policy.third.rawValue.text









share|improve this question
























  • There is a difference between struct Policy and enum Policy: In the first case you have a type with 3 “predefined” values, but a program can create additional values, with arbitrary file names and texts. In the second case you have an enum with 3 values, and that's it.
    – Martin R
    Sep 21 at 4:10
















0














I was in a code review today and someone said, "Why don't you do an enum on that?" I had started to, but conforming to a bunch of protocols seemed like a gigantic PITA and it seemed more error prone than just writing something simple, readable, and maintainable.



That said, I started to fiddle around with it this evening and I can't figure it out.



This is what I've started with:



typealias PolicyType = (filename: String, text: String)

struct Policy {
static let first = PolicyType(filename: "firstFile.txt", text: "text in first file")
static let second = PolicyType(filename: "secondFile.txt", text: "text in second file")
static let third = PolicyType(filename: "thirdFile.txt", text: "text in third file")
}

let thirdPolicyText = Policy.third.text


Is there a more memory efficient, maintainable way to do this with an enum? My primary objective is maintainability.



Below is what I've come up with. It feels hacky and looks like...well, you know:



public struct PolicyType : Equatable, ExpressibleByStringLiteral {
var filename: String
var text: String

//MARK:- Equatable Methods
public static func == (lhs: PolicyType, rhs: PolicyType) -> Bool {
return (lhs.filename == rhs.filename && lhs.text == rhs.text)
}

//MARK:- ExpressibleByStringLiteral Methods
public init(stringLiteral value: String) {
let components = value.components(separatedBy: "|")
self.filename = components[0]
self.text = components[1]
}

public init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
public init(extendedGraphemeClusterLiteral value: String) {
self.init(stringLiteral: value)
}
}

enum Policy: PolicyType {
case first = "testFile1.txt|This is sample text"
case second = "testFile2.txt|This is more sample text"
case third = "testFile3.txt|This is more sample text"
}

Policy.third.rawValue.text









share|improve this question
























  • There is a difference between struct Policy and enum Policy: In the first case you have a type with 3 “predefined” values, but a program can create additional values, with arbitrary file names and texts. In the second case you have an enum with 3 values, and that's it.
    – Martin R
    Sep 21 at 4:10














0












0








0







I was in a code review today and someone said, "Why don't you do an enum on that?" I had started to, but conforming to a bunch of protocols seemed like a gigantic PITA and it seemed more error prone than just writing something simple, readable, and maintainable.



That said, I started to fiddle around with it this evening and I can't figure it out.



This is what I've started with:



typealias PolicyType = (filename: String, text: String)

struct Policy {
static let first = PolicyType(filename: "firstFile.txt", text: "text in first file")
static let second = PolicyType(filename: "secondFile.txt", text: "text in second file")
static let third = PolicyType(filename: "thirdFile.txt", text: "text in third file")
}

let thirdPolicyText = Policy.third.text


Is there a more memory efficient, maintainable way to do this with an enum? My primary objective is maintainability.



Below is what I've come up with. It feels hacky and looks like...well, you know:



public struct PolicyType : Equatable, ExpressibleByStringLiteral {
var filename: String
var text: String

//MARK:- Equatable Methods
public static func == (lhs: PolicyType, rhs: PolicyType) -> Bool {
return (lhs.filename == rhs.filename && lhs.text == rhs.text)
}

//MARK:- ExpressibleByStringLiteral Methods
public init(stringLiteral value: String) {
let components = value.components(separatedBy: "|")
self.filename = components[0]
self.text = components[1]
}

public init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
public init(extendedGraphemeClusterLiteral value: String) {
self.init(stringLiteral: value)
}
}

enum Policy: PolicyType {
case first = "testFile1.txt|This is sample text"
case second = "testFile2.txt|This is more sample text"
case third = "testFile3.txt|This is more sample text"
}

Policy.third.rawValue.text









share|improve this question















I was in a code review today and someone said, "Why don't you do an enum on that?" I had started to, but conforming to a bunch of protocols seemed like a gigantic PITA and it seemed more error prone than just writing something simple, readable, and maintainable.



That said, I started to fiddle around with it this evening and I can't figure it out.



This is what I've started with:



typealias PolicyType = (filename: String, text: String)

struct Policy {
static let first = PolicyType(filename: "firstFile.txt", text: "text in first file")
static let second = PolicyType(filename: "secondFile.txt", text: "text in second file")
static let third = PolicyType(filename: "thirdFile.txt", text: "text in third file")
}

let thirdPolicyText = Policy.third.text


Is there a more memory efficient, maintainable way to do this with an enum? My primary objective is maintainability.



Below is what I've come up with. It feels hacky and looks like...well, you know:



public struct PolicyType : Equatable, ExpressibleByStringLiteral {
var filename: String
var text: String

//MARK:- Equatable Methods
public static func == (lhs: PolicyType, rhs: PolicyType) -> Bool {
return (lhs.filename == rhs.filename && lhs.text == rhs.text)
}

//MARK:- ExpressibleByStringLiteral Methods
public init(stringLiteral value: String) {
let components = value.components(separatedBy: "|")
self.filename = components[0]
self.text = components[1]
}

public init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
public init(extendedGraphemeClusterLiteral value: String) {
self.init(stringLiteral: value)
}
}

enum Policy: PolicyType {
case first = "testFile1.txt|This is sample text"
case second = "testFile2.txt|This is more sample text"
case third = "testFile3.txt|This is more sample text"
}

Policy.third.rawValue.text






swift enum






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 21 at 0:53

























asked Sep 21 at 0:28









Adrian

15129




15129












  • There is a difference between struct Policy and enum Policy: In the first case you have a type with 3 “predefined” values, but a program can create additional values, with arbitrary file names and texts. In the second case you have an enum with 3 values, and that's it.
    – Martin R
    Sep 21 at 4:10


















  • There is a difference between struct Policy and enum Policy: In the first case you have a type with 3 “predefined” values, but a program can create additional values, with arbitrary file names and texts. In the second case you have an enum with 3 values, and that's it.
    – Martin R
    Sep 21 at 4:10
















There is a difference between struct Policy and enum Policy: In the first case you have a type with 3 “predefined” values, but a program can create additional values, with arbitrary file names and texts. In the second case you have an enum with 3 values, and that's it.
– Martin R
Sep 21 at 4:10




There is a difference between struct Policy and enum Policy: In the first case you have a type with 3 “predefined” values, but a program can create additional values, with arbitrary file names and texts. In the second case you have an enum with 3 values, and that's it.
– Martin R
Sep 21 at 4:10










1 Answer
1






active

oldest

votes


















0















  1. The code is using Enum, but it's still using Struct.


  2. The program may be buggy if the text value has character |. Suppose that "testFile1.txt|This is sample text" to "testFile1.txt|This is | sample text".



I have another approach with Enum in this case:



 enum Policy {
case first
case second
case third

var fileName: String {
switch self {
case .first: return "testFile1.txt"
case .second: return "testFile2.txt"
case .third: return "testFile3.txt"
}
}

var text: String {
switch self {
case .first : return "This is sample text"
case .second: return "This is more sample text"
case .third: return "This is more sample text"
}
}
}
print(Policy.first.text)





share|improve this answer























  • Your squiggly brackets ({}) don't match up in your example. Where's the closing bracket for enum Policy {?
    – chicks
    Oct 1 at 11:29










  • @chicks: I updated the example. I hope it more clearly
    – Kien Tran
    Oct 1 at 14:34











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f204096%2fcreating-an-enum-with-a-custom-type-in-swift-4-x%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















  1. The code is using Enum, but it's still using Struct.


  2. The program may be buggy if the text value has character |. Suppose that "testFile1.txt|This is sample text" to "testFile1.txt|This is | sample text".



I have another approach with Enum in this case:



 enum Policy {
case first
case second
case third

var fileName: String {
switch self {
case .first: return "testFile1.txt"
case .second: return "testFile2.txt"
case .third: return "testFile3.txt"
}
}

var text: String {
switch self {
case .first : return "This is sample text"
case .second: return "This is more sample text"
case .third: return "This is more sample text"
}
}
}
print(Policy.first.text)





share|improve this answer























  • Your squiggly brackets ({}) don't match up in your example. Where's the closing bracket for enum Policy {?
    – chicks
    Oct 1 at 11:29










  • @chicks: I updated the example. I hope it more clearly
    – Kien Tran
    Oct 1 at 14:34
















0















  1. The code is using Enum, but it's still using Struct.


  2. The program may be buggy if the text value has character |. Suppose that "testFile1.txt|This is sample text" to "testFile1.txt|This is | sample text".



I have another approach with Enum in this case:



 enum Policy {
case first
case second
case third

var fileName: String {
switch self {
case .first: return "testFile1.txt"
case .second: return "testFile2.txt"
case .third: return "testFile3.txt"
}
}

var text: String {
switch self {
case .first : return "This is sample text"
case .second: return "This is more sample text"
case .third: return "This is more sample text"
}
}
}
print(Policy.first.text)





share|improve this answer























  • Your squiggly brackets ({}) don't match up in your example. Where's the closing bracket for enum Policy {?
    – chicks
    Oct 1 at 11:29










  • @chicks: I updated the example. I hope it more clearly
    – Kien Tran
    Oct 1 at 14:34














0












0








0







  1. The code is using Enum, but it's still using Struct.


  2. The program may be buggy if the text value has character |. Suppose that "testFile1.txt|This is sample text" to "testFile1.txt|This is | sample text".



I have another approach with Enum in this case:



 enum Policy {
case first
case second
case third

var fileName: String {
switch self {
case .first: return "testFile1.txt"
case .second: return "testFile2.txt"
case .third: return "testFile3.txt"
}
}

var text: String {
switch self {
case .first : return "This is sample text"
case .second: return "This is more sample text"
case .third: return "This is more sample text"
}
}
}
print(Policy.first.text)





share|improve this answer















  1. The code is using Enum, but it's still using Struct.


  2. The program may be buggy if the text value has character |. Suppose that "testFile1.txt|This is sample text" to "testFile1.txt|This is | sample text".



I have another approach with Enum in this case:



 enum Policy {
case first
case second
case third

var fileName: String {
switch self {
case .first: return "testFile1.txt"
case .second: return "testFile2.txt"
case .third: return "testFile3.txt"
}
}

var text: String {
switch self {
case .first : return "This is sample text"
case .second: return "This is more sample text"
case .third: return "This is more sample text"
}
}
}
print(Policy.first.text)






share|improve this answer














share|improve this answer



share|improve this answer








edited 16 mins ago









Jamal

30.2k11116226




30.2k11116226










answered Sep 22 at 2:51









Kien Tran

12




12












  • Your squiggly brackets ({}) don't match up in your example. Where's the closing bracket for enum Policy {?
    – chicks
    Oct 1 at 11:29










  • @chicks: I updated the example. I hope it more clearly
    – Kien Tran
    Oct 1 at 14:34


















  • Your squiggly brackets ({}) don't match up in your example. Where's the closing bracket for enum Policy {?
    – chicks
    Oct 1 at 11:29










  • @chicks: I updated the example. I hope it more clearly
    – Kien Tran
    Oct 1 at 14:34
















Your squiggly brackets ({}) don't match up in your example. Where's the closing bracket for enum Policy {?
– chicks
Oct 1 at 11:29




Your squiggly brackets ({}) don't match up in your example. Where's the closing bracket for enum Policy {?
– chicks
Oct 1 at 11:29












@chicks: I updated the example. I hope it more clearly
– Kien Tran
Oct 1 at 14:34




@chicks: I updated the example. I hope it more clearly
– Kien Tran
Oct 1 at 14:34


















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f204096%2fcreating-an-enum-with-a-custom-type-in-swift-4-x%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ellipse (mathématiques)

Quarter-circle Tiles

Mont Emei