2013-12-18 10 views
5

में NSMutableAttributedString को स्टोर करने का सरल तरीकामें NSMutableAttributedString स्टोर करने का प्रयास कर रहा हूं, लेकिन समस्याएं चल रही हैं क्योंकि मेरे NSMutableAttributedString के कुछ विशेषताओं में कोर फाउंडेशन ऑब्जेक्ट्स शामिल हैं जिन्हें संग्रहीत नहीं किया जा सकता है। क्या इस ऑब्जेक्ट को कोरडाटा में मैन्युअल रूप से कुछ गंदे सामान किए बिना स्टोर करने का कोई आसान तरीका है?CoreData

+0

कैसे आप सामान है कि संग्रहित हो सकती है भंडारण के प्रस्ताव हैं? –

+0

@ डंकनक्रेनवेल्ड - यही वह है जो मैं पूछ रहा हूं, अनुमान लगाओ कि यह संभव नहीं है? –

+0

गुणों में आपने जो विशिष्ट चीजें शामिल की हैं, वे क्या हैं? क्या आप उन्हें उपclass नहीं कर सकते हैं और अपने स्वयं के उपclass में संग्रह जोड़ सकते हैं। –

उत्तर

1

आईओएस 5 बाहर होने पर मैंने CoreText का उपयोग करना शुरू किया, और इस प्रकार कोर फाउंडेशन मानों को विशेषताओं के रूप में उपयोग किया। हालांकि मैं अब महसूस करते हैं कि जब से iOS6 बाहर आया था, मैं अब NSForegroundColorAttributeName, NSParagraphStyleAttributeName, NSFontAttributeName, आदि गुण शब्दकोश में उपयोग कर सकते हैं, और उन कुंजियों UIColor, NSMutableParagraphStyle, और UIFont जो संग्रहीत किया जा सकता है जैसे वस्तुओं के साथ कर रहे।

यह एक अनुरोध लाने का निर्माण करने के लिए संभव नहीं है/विधेय कि NSAttributedString वस्तु की सामग्री प्रश्नों:

5

NSMutableAttributedStringNSCoding के अनुरूप है, जिसका अर्थ है यह जानता है कि कैसे एक NSData से/करने के लिए खुद को परिवर्तित करने के लिए और एक प्रोटोकॉल कोर डाटा जानता है कि कैसे उपयोग करने के लिए के माध्यम से ऐसा करता है।

विशेषता "ट्रांसफॉर्म करने योग्य" बनाएं, और उसके बाद केवल जिम्मेदार स्ट्रिंग असाइन करें। चूंकि यह परिवर्तनीय है, कोर डेटा NSCoding का उपयोग NSData में कनवर्ट करने के लिए करेगा जब आप कोई मान निर्दिष्ट करते हैं, और इसे पढ़ते समय इसे एक जिम्मेदार स्ट्रिंग में परिवर्तित करने के लिए।

नोट, आप इस क्षेत्र पर फ़िल्टर फ़िल्टर करने के लिए एक अनुमान का उपयोग करने में सक्षम नहीं होंगे। लेकिन इसे संग्रहित करना और पुनर्प्राप्त करना सरल है।

+0

यह एनएससीओडी के अनुरूप है, लेकिन यह संभव है कि इसकी संपत्तियां (गुण) नहीं होंगी। –

1

खैर मुझे यकीन है कि आप जिम्मेदार ठहराया स्ट्रिंग के साथ क्या करना कोशिश कर रहे हैं नहीं कर रहा हूँ, लेकिन अगर यह स्वरूपित पाठ तो आप NSFont, आदि का उपयोग नहीं कर सकते ..

एक नज़र यहाँ http://ossh.com.au/design-and-technology/software-development ले लो, मैं कुछ सामान तैनात uitextview और nstextview के साथ स्वरूपण शैलियों और छवियों पर, लेकिन ज्यादातर यह जिम्मेदार तारों के बारे में है।

यह सामान सभी कोर डेटा में संग्रहीत है।

4

जबकि इसके बाद के संस्करण जवाब सही है, यह एक बड़ा नुकसान है!

इस तरह एक विधेय जब मार डाला एक अपवाद का कारण होगा:

[NSPredicate predicateWithFormat:@"(content CONTAINS[cd] %@)", @"test"]]; 

कोर डाटा में एक 'fetchable' NSAttributedString संग्रहीत करने के लिए की जरूरत है है दो भागों में NSAttributedString गिरा करने के लिए: एक NSString पक्ष (जो हो सकता है प्राप्त) और एक NSData पक्ष, जो गुणों को स्टोर करता है।

इस विभाजन कोर डाटा इकाई इन तीनों विशेषताओं बनाने के द्वारा प्राप्त किया जा सकता:

  1. एक छाया NSString विशेषता ('contentString')
  2. एक छाया NSData विशेषता ('contentAttributes')
  3. एक 'अपरिभाषित' क्षणिक विशेषता ('सामग्री')

कस्टम इकाइयों वर्ग में 'सामग्री' ने अपनी छाया से बनाए गए गुणों को श्रेय दिया और विशेषता में परिवर्तन भी दर्पण हैं अपनी छाया के लिए एड।

हैडर फ़ाइल:

/** 
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

संबंधित मुद्दे