如何获取 UIColor 的所有 Color 方法? [英] How to get all Color methods of UIColor?
问题描述
UIColor
有blackColor
、whiteColor
等颜色方法.我想获得它们的颜色列表,例如:['blackColor','whiteColor'...]
.但是在检查了 method_list
、ivar_list
和 property_list
之后,我发现列表中没有颜色方法.代码使用 Swift
编写如下:
UIColor
has color methods such as blackColor
, whiteColor
and so on. I would like to get a color list of them like:['blackColor','whiteColor'...]
. But after checking the method_list
, ivar_list
and property_list
, I find no color method in the lists. The code is written in Swift
as following:
//: Playground - noun: a place where people can play
import UIKit
var ivarCount: UInt32 = 0
var propertyCount: UInt32 = 0
var methodCount: UInt32 = 0
let ivars = class_copyIvarList(UIColor.self, &ivarCount)
let properties = class_copyPropertyList(UIColor.self, &propertyCount)
let methods = class_copyMethodList(UIColor.self, &methodCount)
var ivarNames: [String] = []
var propertyNames: [String] = []
var methodNames: [String] = []
for var i = 0; i < Int(ivarCount); ++i {
let ivar = ivars[i]
let name = ivar_getName(ivar)
ivarNames.append(String.fromCString(name)!);
}
for var i = 0; i < Int(propertyCount); ++i {
let p = properties[i]
let name = property_getName(p)
propertyNames.append(String.fromCString(name)!)
}
for var i = 0; i < Int(methodCount); ++i {
let m = methods[i]
let name = sel_getName(method_getName(m))
methodNames.append(String.fromCString(name)!)
}
NSLog("ivars:%@",ivarNames)
NSLog("properties:%@", propertyNames)
NSLog("methods:%@", methodNames)
结果如下:
2016-01-18 11:26:25.531 MyPlayground[42239:4975071] ivar:(
"_systemColorName"
)
2016-01-18 11:26:25.531 MyPlayground[42239:4975071] properties:(
CGColor,
CIColor,
systemColorName
)
2016-01-18 11:26:25.532 MyPlayground[42239:4975071] method:(
"initWithColorLiteralRed:green:blue:alpha:",
classForCoder,
hash,
"isEqual:",
set,
"initWithHue:saturation:brightness:alpha:",
"_getWhite:alpha:",
"_systemColorName",
"initWithWhite:alpha:",
"_getRed:green:blue:alpha:",
"_colorBlendedWithColor:",
styleString,
isPatternColor,
"getHue:saturation:brightness:alpha:",
CIColor,
"initWithPatternImage:",
"_setSystemColorName:",
"_luminance",
"_colorBlendedWithColor:compositingFilter:",
"_isSimilarToColor:withinPercentage:",
"_colorDifferenceFromColor:",
"_luminanceDifferenceFromColor:",
"_colorBlendedWithColors:",
dealloc,
"copyWithZone:",
"encodeWithCoder:",
"initWithCoder:",
cgColor,
"initWithCGColor:",
"initWithRed:green:blue:alpha:",
CGColor,
"getRed:green:blue:alpha:",
setFill,
"colorWithAlphaComponent:",
setStroke,
alphaComponent,
"getWhite:alpha:",
"initWithCIColor:"
)
推荐答案
那是因为颜色方法是类方法,绑定到 UIColor
的元类.class_copyMethodList
返回作为参数传递的类的实例方法.
That's because the color methods are class methods, which are bound to UIColor
's metaclass. And class_copyMethodList
returns the instance methods of the class passed as argument.
您可以通过在UIColor
上调用object_getClass()
来获取元类,因此您只需要更改一行即可获取UIColor的所有类方法代码>:
You can obtain the metaclass by calling object_getClass()
on UIColor
, so you'll need to change only one line to get all class methods of UIColor
:
let methods = class_copyMethodList(object_getClass(UIColor.self), &methodCount)
object_getClass()
作用于 Class
值,就像在 Objective-C
中一样,每个类都有一个 isa
指针,这使它有资格成为一个对象.
object_getClass()
works on Class
values as in Objective-C
every class has an isa
pointer, which qualifies it for being an object.
这篇关于如何获取 UIColor 的所有 Color 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!