构建NSMutableString的问题

时间:2014-12-10 08:44:26

标签: ios objective-c nsmutablestring

我必须构建一个字符串以便传输到我们的服务器之一,并且我开始在此过程中遇到故障,我看到问题所在,但我无法弄清楚如何解决这个问题。如果有人有任何想法,我很乐意听取他们的意见。

这是我的整个方法,它将类转换为xml字符串。

- (NSData *)classToXML
{
    NSMutableString *xml = [@"<JournalData xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" mutableCopy];
    // 150
    if( _journalSubject.length > 150 )
        _journalSubject = [_journalSubject stringByReplacingCharactersInRange:NSMakeRange(151, _journalSubject.length - 150) withString:@""];
    [xml appendFormat:@"<JournalSubject>%@</JournalSubject>", _journalSubject];
    if( self.recipientList == nil )
         [xml appendFormat:@"<RecipientList isNull=\"True\"/>"];
    else
    {
        [xml appendString:@"<RecipientList>"];
        for( CTIJournalRecipients *r in self.recipientList )
             [xml appendString:[r classToXML]];
        [xml appendString:@"</RecipientList>"];
    }
    [xml appendFormat:@"<JournalSysCategoryid1>%@</JournalSysCategoryid1>", _sysCategoryid1];
    [xml appendFormat:@"<JournalSysCategoryid2>%@</JournalSysCategoryid2>", _sysCategoryid2];
    [xml appendFormat:@"<Userid>%@</Userid>", _userid];
    [xml appendFormat:@"<Deptid>%@</Deptid>", _deptid];
    [xml appendFormat:@"<Activityid>%@</Activityid>", _activityid ? _activityid : @(0)];
    // 50
    if( _deptDescription.length > 50 )
       _deptDescription = [_deptDescription stringByReplacingCharactersInRange:NSMakeRange(51, _deptDescription.length - 50) withString:@""];
    [xml appendFormat:@"<DeptDescription>%@</DeptDescription>", _deptDescription];
    [xml appendFormat:@"<SecLevel>%@</SecLevel>", _securityLevel];
    [xml appendFormat:@"<AttachedDocs>%@</AttachedDocs>", _attachedDocs];
    [xml appendFormat:@"<Priority>%@</Priority>", _priority];
    // 60
    if( _associatedActivity.length > 60 )
        _associatedActivity = [_associatedActivity stringByReplacingCharactersInRange:NSMakeRange(61, _associatedActivity.length - 60) withString:@""];
    [xml appendFormat:@"<AssociatedActivity>%@</AssociatedActivity>", _associatedActivity.length > 0 ? _associatedActivity : @"No Activity"];
    [xml appendFormat:@"<JournalPerson>%@</JournalPerson>", _journalPerson];
    [xml appendFormat:@"<JournalDoc>%@</JournalDoc>", _journalDoc];
    [xml appendFormat:@"<JournalTransmission>%@</JournalTransmission>", _recipientList.count > 0 ? @"Y" : @"N"];
    [xml appendFormat:@"<JournalAssignment>%@</JournalAssignment>", _journalAssignment];
    [xml appendFormat:@"<JournalTag>%@</JournalTag>", _journalTag];
    [xml appendString:@"<JournalMemo>%@</JournalMemo>", _journalMemo];
    [xml appendFormat:@"<CreateUserFullname>%@</CreateUserFullname>", _createUserFullName];
    [xml appendFormat:@"<CreateUserID>%@</CreateUserID>", _userid];
    [xml appendFormat:@"<Macroid>%@</Macroid>", _macroid];
    [xml appendFormat:@"<sContext>%@</sContext>", [_sContext capitalizedString]];
    [xml appendFormat:@"<iDetail1>%@</iDetail1>", _iDetail1 ? _iDetail1 : @(0)];
    [xml appendFormat:@"<iDetail2>0</iDetail2>"];
    [xml appendFormat:@"<iDetail3>0</iDetail3>"];
    [xml appendFormat:@"<iDetail4>0</iDetail4>"];
    [xml appendFormat:@"<sConcerning>%@</sConcerning>", _sConcerning];
    [xml appendFormat:@"<sFaxCover>%@</sFaxCover>", _sFaxCover];
    [xml appendFormat:@"<bTransmit>%@</bTransmit>", _recipientList.count > 0 ? @"true" : @"false" ];
    // 18
    if( _journalHeaderAccess.length > 18 )
        _journalHeaderAccess = [_journalHeaderAccess stringByReplacingCharactersInRange:NSMakeRange(19, _journalHeaderAccess.length - 18) withString:@""];
    [xml appendFormat:@"<JournalHeaderAccess>%@</JournalHeaderAccess>", _journalHeaderAccess.length > 0 ? _journalHeaderAccess : _sContext];
    [xml appendFormat:@"<vParams isNull=\"True\"></vParams>" ];
    [xml appendFormat:@"<DocumentList isNull=\"True\"></DocumentList>"];
    [xml appendFormat:@"<iJournalReplyHeaderid>%@</iJournalReplyHeaderid>", _iJournalReplyHeader];
    [xml appendFormat:@"<iReplyToTransmissionid>%@</iReplyToTransmissionid>", _iReplyToTransmissionid];
    [xml appendFormat:@"</JournalData>"];
    //NSString *xmlFinal = [NSString stringWithFormat:xml, [_journalMemo dataUsingEncoding:NSUTF8StringEncoding]];
    //CLSNSLog(@"%@", xml);
    return xmlEncoded;
}

我的问题在于JournalMemo字段,当我使用这个字符串时:

n\n\n12/9/14, 11:26 PM: Someone Else wrote:\n\nTest reply please.\r\n\r\nThanks!\r\n\r\n\r\n

我尝试了几种不同的方法将该备忘录字段插入到xml中,这就是在我插入JournalMemo令牌之前xml的样子:

<JournalData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<JournalSubject>RE: Test reply please</JournalSubject>
<RecipientList>
     <JournalRecipients>
         <RecipientMethod>rmTo</RecipientMethod>
         <CommID>6711</CommID>
         <CommTypeID>52</CommTypeID>
         <CommName>someperson@example.com</CommName>
         <PersonID>2656</PersonID>
         <PersonName>Some Person</PersonName>
         <PersonCategoryID>0</PersonCategoryID>
         <PersonCategory />
         <PersonType />
         <ToType />
    </JournalRecipients>
</RecipientList>
<JournalSysCategoryid1>2000</JournalSysCategoryid1>
<JournalSysCategoryid2>490</JournalSysCategoryid2>
<Userid>2656</Userid>
<Deptid>0</Deptid>
<Activityid>0</Activityid>
<DeptDescription>No Department Selected</DeptDescription>
<SecLevel>1</SecLevel>
<AttachedDocs>N</AttachedDocs>
<Priority>NORMAL</Priority>
<AssociatedActivity>No Activity</AssociatedActivity>
<JournalPerson>N</JournalPerson>
<JournalDoc>N</JournalDoc>
<JournalTransmission>Y</JournalTransmission>
<JournalAssignment>N</JournalAssignment>
<JournalTag>X</JournalTag>
<JournalMemo>%@</JournalMemo>
<CreateUserFullname>Someone Else</CreateUserFullname>
<CreateUserID>2676</CreateUserID>
<Macroid>0</Macroid>
<sContext>(null)</sContext>
<iDetail1>0</iDetail1>
<iDetail2>0</iDetail2>
<iDetail3>0</iDetail3>
<iDetail4>0</iDetail4>
<sConcerning>(null)</sConcerning>
<sFaxCover>Primary Cover Sheet</sFaxCover>
<bTransmit>true</bTransmit>
<JournalHeaderAccess>(null)</JournalHeaderAccess>
<vParams isNull="True"></vParams>
<DocumentList isNull="True"></DocumentList> 
<iJournalReplyHeaderid>557762</iJournalReplyHeaderid>
<iReplyToTransmissionid>1170918</iReplyToTransmissionid>
</JournalData>

插入字符串后,这就是xml变量的样子:

<JournalData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<JournalSubject>RE: Test reply please</JournalSubject>
<RecipientList>
     <JournalRecipients>
         <RecipientMethod>rmTo</RecipientMethod>
         <CommID>6711</CommID>
         <CommTypeID>52</CommTypeID>
         <CommName>someperson@example.com</CommName>
         <PersonID>2656</PersonID>
         <PersonName>Some Person</PersonName>
         <PersonCategoryID>0</PersonCategoryID>
         <PersonCategory />
         <PersonType />
         <ToType />
    </JournalRecipients>
</RecipientList>
<JournalSysCategoryid1>2000</JournalSysCategoryid1>
<JournalSysCategoryid2>490</JournalSysCategoryid2>
<Userid>2656</Userid>
<Deptid>0</Deptid>
<Activityid>0</Activityid>
<DeptDescription>No Department Selected</DeptDescription>
<SecLevel>1</SecLevel>
<AttachedDocs>N</AttachedDocs>
<Priority>NORMAL</Priority>
<AssociatedActivity>No Activity</AssociatedActivity>
<JournalPerson>N</JournalPerson>
<JournalDoc>N</JournalDoc>
<JournalTransmission>Y</JournalTransmission>
<JournalAssignment>N</JournalAssignment>
<JournalTag>X</JournalTag>
<JournalMemo>


12/9/14, 11:26 PM: Someone Else wrote:

Test reply please.

Thanks!

我并不完全确定是什么导致了这个问题,或者我如何解决它。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

\n导致问题,你需要逃避它:

使用:

_journalMemo = [_journalMemo stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];

答案 1 :(得分:0)

answer帮助我克服了我的问题。

我最终使用了这段代码,因为显然UTF8编码存在一些问题。

NSString *str = [NSString stringWithUTF8String: [_journalMemo cStringUsingEncoding: [ NSString defaultCStringEncoding ] ] ];
[xml appendFormat:@"<JournalMemo>%@</JournalMemo>", str];