Swift从常规状态获取类实例以调用类方法

时间:2018-11-27 13:21:13

标签: swift instance class-method

给定一个常规实例,我该如何调用类方法?

在ObjC中,我习惯于使用self.class

import json            # <-- Import json
import urllib.request
import urllib.parse

d = {"spam": 1, "eggs": 2, "bacon": 0}
data = json.dumps(d)   # <-- Dump the dictionary as JSON
data = data.encode()
req = urllib.request.Request("http://localhost:5000/random", data)
req.add_header('Content-Type', 'application/json')
req.add_header('Authorization', 12345)
with urllib.request.urlopen(req) as f:
    print(f.read().decode('utf-8'))

想法是,someClassMethod可以在超类中被覆盖,因此使用直接类名不是一种选择,我必须调用实例的类方法

但我在Swift中找不到等效项

0 个答案:

没有答案