简体   繁体   English

iOS swift 中正确的对象类型

[英]Correct object type in iOS swift

I need to choose a type for holding object.我需要选择一种类型来容纳对象。 But I need some help.但我需要一些帮助。

My object should have 3 property:我的对象应该有 3 个属性:

  • small小的
  • medium中等的
  • max最大限度

Each of them will hold URL他们每个人都会持有 URL

Also my object should have Preferred property.我的对象也应该有首选属性。 Which will be assigned as small, medium or max.这将被分配为小、中或最大。 And Preferred should return URL from selected property.并且首选应该从选定的属性返回 URL。

For example if I assign:例如,如果我分配:

small = url1
medium = url2
max = url3

//Here also I have global settings class. //这里我也有全局设置类。 And in that I should choose to return from preferred - max urls.并且我应该选择从首选 - 最大网址返回。 And for every insnanse preferred should return max.对于每个疯狂的首选应该返回最大值。

Class.preferred = Type.max //something like that
print(instanse.preferred) //printed url3

How can I archive that?我该如何存档? I can not use Class here, but I am not sure can I archive that with enum?我不能在这里使用 Class,但我不确定我可以用 enum 归档它吗?

Sample which does not work不起作用的示例

enum VideoType: String {
case min
case med
case max
}


class VideoInstanse {
var min: VideoType
var med: VideoType
var max: VideoType

static var preferred:VideoType!

init(dictionary: [Int : String]) {
    min.rawValue = dictionary[1] //error
    med.rawValue = dictionary[2]
    max.rawValue = dictionary[3]
}
}

You might be interested in using an enum in this case (if I understand your question correctly)在这种情况下,您可能有兴趣使用枚举(如果我正确理解您的问题)

enum Endpoint: String {
  var baseURLString: String {
    get {
      return "https://myurl.com?size=".appending(self.rawValue)
    }
  }

  case small
  case medium
  case max

  var url: URL? {
    return URL(string: baseURLString)
  }
}


print(Endpoint.small.url!)
print(Endpoint.medium.url!)
print(Endpoint.max.url!)

The output is:输出是:

https://myurl.com?size=small
https://myurl.com?size=medium
https://myurl.com?size=max

Struct, maybe?结构,也许?

struct Size {
    var small : NSURL = NSURL()
    var medium : NSURL = NSURL()
    var large : NSURL = NSURL()
}
class VideoInstanse {
    var properties = Size()


   func someMethod() {
    self.properties.small = url1
    self.properties.medium = url2
    self.properties.large = url3
   }
}

You can also use it Outside the class somewhere else like so:您也可以在课堂之外的其他地方使用它,如下所示:

var instance = VideoInstance()
instance.properties.small...
// and so on...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM