[英]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.