There are a couple of problems with your architecture. You need to know when and why you need to use extensions and protocols and how you should structure your code blocks.
- If your type must comply with this protocol, feel free to use it to make sure that you set your own standards. I don't even see this in the github project you talked about.
- Extension is a good way to have a primitive type and extend its functionality in other parts of the project. It makes no sense to me why you should extend the type immediately after the declaration. A good use case is that the String type has been extended to support URL-encoded values:
private extension String { var URLEscapedString: String { return self.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())! } }
- When you use this type of switch lock
switch self { case .Zen: return "/zen" case .UserProfile(let name): return "/users/\(name.URLEscapedString)" case .UserRepositories(let name): return "/users/\(name.URLEscapedString)/repos" }
The value in the case must be a member of self. why can't he find the type. the type is declared inside an Instagram enumeration, but it does not store the value in self. It retains value inside Media. So move your media-related feature to your Media ad and access them. So, I mean Media. Here's the full working code for me:
private extension String { var URLEscapedString: String { return self.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())! } }
public enum Instagram {
public enum Media { case Search(String) var path:String { switch self { case Media.Search(let keyword): return "/media/search/\(keyword.URLEscapedString)" } } }
}
var me = Instagram.Media.Search( "me" ) (me.path)
code>
- As one of the tips at each stage of building the entire architecture, just ask yourself the question, does this part of the code belong to this type or should it be publicly available. In this case, it makes sense to move the search to Media because you are looking for media. You can add the same template for something like User and do a search under a user that returns a different value.
MKoosej
source share