Objc获取类的所有属性失败

时间:2018-02-06 13:30:52

标签: ios objective-c corespotlight

我想创建一个CSSearchableItem,其中包含所有可用属性的attributeSet。

为此,我试图以下列方式获取CSSearchableItemAttributeSet类的所有属性:

NSMutableArray * allPropertyNames(Class c)
{
    unsigned count;
    objc_property_t *properties = class_copyPropertyList([CSSearchableItemAttributeSet class], &count);

    NSMutableArray *rv = [NSMutableArray array];

    unsigned i;
    for (i = 0; i < count; i++)
    {
        objc_property_t property = properties[i];
        NSString *name = [NSString stringWithUTF8String:property_getName(property)];
        [rv addObject:name];
    }

    free(properties);

    return rv;
}

问题是我得到以下结果:

HTMLContentDataNoCopy,
textContentNoCopy,
accountType,
textSelected,
subtitle,
userTags,
albumPersistentID,
adamID,
extendedContentRating,
fileIdentifier,
parentFileIdentifier,
filename,
documentIdentifier,
dataOwnerType,
existingThread,
partiallyDownloaded,
queryResultMatchedFields,
uniqueIdentifier,
bundleID,
protectionClass,
expirationDate,
userActivityType,
queryResultRelevance,
applicationName,
contentSnippet,
relatedAppBundleIdentifier,
mailAttachmentNames,
mailAttachmentTypes,
mailAttachmentKinds,
mailDateReceived,
mailDateLastViewed,
mailFlagged,
mailFlagColor,
mailRead,
mailRepliedTo,
mailPriority,
mailGMailLabels,
mailMessageID,
mailCategory,
mailConversationID,
readerView,
textContentDataSource,
fileProviderID,
fileItemID,
parentFileItemID,
ownerName,
ownerIdentifier,
lastEditorName,
lastEditorIdentifier,
fileProviderDomaindentifier,
fileProviderUserInfoKeys,
fileProviderUserInfoValues,
trashed,
shared,
uploaded,
uploading,
uploadError,
downloading,
downloadError,
extraData,
favoriteRank,
subItemCount,
sharedItemCurrentUserRole,
versionIdentifier,
downloadingStatus,
lastApplicationLaunchedDate,
isPlaceholder,
mutableAttributes,
customAttributes,
attributes,
searchableItemFlags,
decoder,
contentDecoder,
codedAttributes,
codedCustomAttributes,
contentObj,
hasCodedCustomAttributes

这些都不是我想要的属性。

有谁知道如何获得这个?

1 个答案:

答案 0 :(得分:2)

这似乎是在CSSearchableItemAttributeSet类上声明的属性列表。什么出乎意料?

虽然Objective-C具有内省功能,但所述功能并非旨在在运行时用于以这种方式推断或细分类的功能。

即。内省驱动编程在很大程度上是不受欢迎的,除了一些非常具体的例子,如委托。

在这种情况下,您可能会在采用CoreData的正式建模功能(或类似解决方案)或创建包含您要宣传的属性列表的类方法方面取得成功。

总的来说,如果你通常使用相对静态的调用站点(即[someObj myProperty]),而不是试图抽象出来,那么你的代码很可能不易出错。