में NSMutableAttributedString को स्टोर करने का सरल तरीकामें NSMutableAttributedString
स्टोर करने का प्रयास कर रहा हूं, लेकिन समस्याएं चल रही हैं क्योंकि मेरे NSMutableAttributedString
के कुछ विशेषताओं में कोर फाउंडेशन ऑब्जेक्ट्स शामिल हैं जिन्हें संग्रहीत नहीं किया जा सकता है। क्या इस ऑब्जेक्ट को कोरडाटा में मैन्युअल रूप से कुछ गंदे सामान किए बिना स्टोर करने का कोई आसान तरीका है?CoreData
CoreData
उत्तर
आईओएस 5 बाहर होने पर मैंने CoreText
का उपयोग करना शुरू किया, और इस प्रकार कोर फाउंडेशन मानों को विशेषताओं के रूप में उपयोग किया। हालांकि मैं अब महसूस करते हैं कि जब से iOS6 बाहर आया था, मैं अब NSForegroundColorAttributeName
, NSParagraphStyleAttributeName
, NSFontAttributeName
, आदि गुण शब्दकोश में उपयोग कर सकते हैं, और उन कुंजियों UIColor
, NSMutableParagraphStyle
, और UIFont
जो संग्रहीत किया जा सकता है जैसे वस्तुओं के साथ कर रहे।
यह एक अनुरोध लाने का निर्माण करने के लिए संभव नहीं है/विधेय कि NSAttributedString वस्तु की सामग्री प्रश्नों:
NSMutableAttributedString
NSCoding
के अनुरूप है, जिसका अर्थ है यह जानता है कि कैसे एक NSData
से/करने के लिए खुद को परिवर्तित करने के लिए और एक प्रोटोकॉल कोर डाटा जानता है कि कैसे उपयोग करने के लिए के माध्यम से ऐसा करता है।
विशेषता "ट्रांसफॉर्म करने योग्य" बनाएं, और उसके बाद केवल जिम्मेदार स्ट्रिंग असाइन करें। चूंकि यह परिवर्तनीय है, कोर डेटा NSCoding
का उपयोग NSData
में कनवर्ट करने के लिए करेगा जब आप कोई मान निर्दिष्ट करते हैं, और इसे पढ़ते समय इसे एक जिम्मेदार स्ट्रिंग में परिवर्तित करने के लिए।
नोट, आप इस क्षेत्र पर फ़िल्टर फ़िल्टर करने के लिए एक अनुमान का उपयोग करने में सक्षम नहीं होंगे। लेकिन इसे संग्रहित करना और पुनर्प्राप्त करना सरल है।
यह एनएससीओडी के अनुरूप है, लेकिन यह संभव है कि इसकी संपत्तियां (गुण) नहीं होंगी। –
खैर मुझे यकीन है कि आप जिम्मेदार ठहराया स्ट्रिंग के साथ क्या करना कोशिश कर रहे हैं नहीं कर रहा हूँ, लेकिन अगर यह स्वरूपित पाठ तो आप NSFont, आदि का उपयोग नहीं कर सकते ..
एक नज़र यहाँ http://ossh.com.au/design-and-technology/software-development ले लो, मैं कुछ सामान तैनात uitextview और nstextview के साथ स्वरूपण शैलियों और छवियों पर, लेकिन ज्यादातर यह जिम्मेदार तारों के बारे में है।
यह सामान सभी कोर डेटा में संग्रहीत है।
जबकि इसके बाद के संस्करण जवाब सही है, यह एक बड़ा नुकसान है!
इस तरह एक विधेय जब मार डाला एक अपवाद का कारण होगा:
[NSPredicate predicateWithFormat:@"(content CONTAINS[cd] %@)", @"test"]];
कोर डाटा में एक 'fetchable' NSAttributedString संग्रहीत करने के लिए की जरूरत है है दो भागों में NSAttributedString गिरा करने के लिए: एक NSString पक्ष (जो हो सकता है प्राप्त) और एक NSData पक्ष, जो गुणों को स्टोर करता है।
इस विभाजन कोर डाटा इकाई इन तीनों विशेषताओं बनाने के द्वारा प्राप्त किया जा सकता:
- एक छाया NSString विशेषता ('contentString')
- एक छाया NSData विशेषता ('contentAttributes')
- एक 'अपरिभाषित' क्षणिक विशेषता ('सामग्री')
कस्टम इकाइयों वर्ग में 'सामग्री' ने अपनी छाया से बनाए गए गुणों को श्रेय दिया और विशेषता में परिवर्तन भी दर्पण हैं अपनी छाया के लिए एड।
हैडर फ़ाइल:
/**
MMTopic
*/
@interface MMTopic : _MMTopic {}
@property (strong, nonatomic) NSAttributedString* content;
@end
कार्यान्वयन फ़ाइल:
/**
MMTopic (PrimitiveAccessors)
*/
@interface MMTopic (PrimitiveAccessors)
- (NSAttributedString *)primitiveContent;
- (void)setPrimitiveContent:(NSAttributedString *)pContent;
@end
/**
MMTopic
*/
@implementation MMTopic
static NSString const* kAttributesDictionaryKey = @"AttributesDictionary";
static NSString const* kAttributesRangeKey = @"AttributesRange";
/*
awakeFromFetch
*/
- (void)awakeFromFetch {
[super awakeFromFetch];
// Build 'content' from its shadows 'contentString' and 'contentAttributes'
NSString* string = self.contentString;
NSMutableAttributedString* content = [[NSMutableAttributedString alloc] initWithString:string];
NSData* attributesData = self.contentAttributes;
NSArray* attributesArray = nil;
if (attributesData) {
NSKeyedUnarchiver* decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:attributesData];
attributesArray = [[NSArray alloc] initWithCoder:decoder];
}
if ((content) &&
(attributesArray.count)) {
for (NSDictionary* attributesDictionary in attributesArray) {
//NSLog(@"%@: %@", NSStringFromRange(((NSValue*)attributesDictionary[kAttributesRangeKey]).rangeValue), attributesDictionary[kAttributesDictionaryKey]);
[content addAttributes:attributesDictionary[kAttributesDictionaryKey]
range:((NSValue*)attributesDictionary[kAttributesRangeKey]).rangeValue];
}
[self setPrimitiveContent:content];
}
}
/*
content
*/
@dynamic content;
/*
content (getter)
*/
- (NSAttributedString *)content {
[self willAccessValueForKey:@"content"];
NSAttributedString* content = [self primitiveContent];
[self didAccessValueForKey:@"content"];
return content;
}
/*
content (setter)
*/
- (void)setContent:(NSAttributedString *)pContent {
[self willChangeValueForKey:@"content"];
[self setPrimitiveValue:pContent forKey:@"content"];
[self didChangeValueForKey:@"content"];
// Update the shadows
// contentString
[self setValue:pContent.string
forKey:@"contentString"];
// contentAttributes
NSMutableData* data = [NSMutableData data];
NSKeyedArchiver* coder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
NSMutableArray* attributesArray = [NSMutableArray array];
[pContent enumerateAttributesInRange:NSMakeRange(0, pContent.length)
options:0
usingBlock:^(NSDictionary* pAttributesDictionary, NSRange pRange, BOOL* prStop) {
//NSLog(@"%@: %@", NSStringFromRange(pRange), pAttributesDictionary);
[attributesArray addObject:@{
kAttributesDictionaryKey: pAttributesDictionary,
kAttributesRangeKey: [NSValue valueWithRange:pRange],
}];
}];
[attributesArray encodeWithCoder:coder];
[coder finishEncoding];
[self setValue:data
forKey:@"contentAttributes"];
}
@end
ला रहा है अब तक किया जा सकता है:
[NSPredicate predicateWithFormat:@"(contentString CONTAINS[cd] %@)", @"test"]];
NSAttributedString के लिए किसी भी पहुँच इस प्रकार है जबकि:
textField.attributedText = pTopic.content;
साथ कोर डाटा में 'गैर मानक विशेषताएँ' काम करने के लिए नियमों को यहाँ प्रलेखित रहे हैं: Apple docs
- 1. CoreData
- 2. सीमा CoreData
- 3. CoreData प्रवास -
- 4. CoreData संदर्भ बचत
- 5. CoreData 1570 त्रुटि कोड
- 6. SQLite और CoreData
- 7. कुछ CoreData रिश्तों एप्लिकेशन
- 8. आईफोन - coredata आवेदन निर्देशिका
- 9. iPhone: CoreData, 2 कॉलम
- 10. CoreData संबंध रिटर्न एक _NSCoreDataTaggedObjectID
- 11. CoreData हटाना नियम भ्रम, xcode
- 12. ग्रांड सेंट्रल डिस्पैच (GCD) CoreData
- 13. CoreData ManagedObjectContext रिकर्सिव सहेजें त्रुटि
- 14. () iPhone/CoreData में अनसुलझे त्रुटियों
- 15. CoreData त्रुटि मुझे पागल गाड़ी चला रहा है ... CoreData: गंभीर अनुप्रयोग त्रुटि। NSFetchedResultsController
- 16. JSON से लगातार डेटा स्टोर (CoreData, आदि)
- 17. coredata - एक सरणी में एक विशेषता
- 18. CoreData (iPhone के लिए) भंडारण छवियों
- 19. CoreData के साथ यूनिटटेस्ट कैसे करें?
- 20. स्विफ्ट CoreData DictionaryResultType निम्नलिखित कोड में
- 21. CoreData: त्रुटि: -addPersistentStoreWithType: SQLite विन्यास: (शून्य)
- 22. जांचें कि क्या CoreData विशेषता खाली है
- 23. स्टोर लेकिन CoreData को NSManagedObject सहेजें नहीं?
- 24. CoreData SQL क्वेरी कैसे लॉग इन करें?
- 25. CoreData त्रुटि "डेटा: <fault>"
- 26. हटाया जा रहा है या CoreData
- 27. क्यों CoreData अशक्त सरणी लौटा रहा है, जबकि मैं मेज
- 28. CoreData आदेश दिया संबंध - NSFetchRequest का उपयोग कर unfaulting बैच
- 29. CoreData संदर्भ के बाद ऑब्जेक्ट्स कैसे प्राप्त करें
- 30. KVO addObserver CoreData ऑब्जेक्ट के एक-से-कई रिश्ते
कैसे आप सामान है कि संग्रहित हो सकती है भंडारण के प्रस्ताव हैं? –
@ डंकनक्रेनवेल्ड - यही वह है जो मैं पूछ रहा हूं, अनुमान लगाओ कि यह संभव नहीं है? –
गुणों में आपने जो विशिष्ट चीजें शामिल की हैं, वे क्या हैं? क्या आप उन्हें उपclass नहीं कर सकते हैं और अपने स्वयं के उपclass में संग्रह जोड़ सकते हैं। –