【问题标题】:Objc get all properties of class failsobjc 获取类的所有属性失败
【发布时间】:2018-07-16 14:08:44
【问题描述】:

我想创建一个包含所有可用属性的属性集的 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

这些都不是我要寻找的属性。

有谁知道如何获得这个?

【问题讨论】:

  • 您在寻找哪些房产?

标签: ios objective-c corespotlight


【解决方案1】:

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


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

即除了委托等一些非常具体的例子外,内省驱动的编程在很大程度上是不鼓励的。

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

不过,总体而言,如果您通常使用相对静态的调用站点(即 [someObj myProperty])而不是尝试抽象,那么您的代码可能不太容易出错。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-20
    • 2016-10-06
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多