繁体   English   中英

Objc获取类的所有属性失败

[英]Objc get all properties of class fails

我想使用所有可用属性的attributeSet创建一个CSSearchableItem。

为此,我尝试通过以下方式获取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

这些都不是我一直在寻找的属性。

有谁知道如何得到这个?

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


尽管Objective-C具有自省功能,但这些功能并非旨在在运行时用于以这种方式推论或细分类的功能。

即,自省驱动的编程在很大程度上不建议使用,除非有一些非常具体的示例,例如委托。

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

总体而言,如果您通常使用相对静态的调用站点(例如[someObj myProperty]),而不是尝试抽象化代码,则您的代码很可能不会出现错误。

暂无
暂无

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

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