Search code examples
ioscore-datansstringwhitespaceline-breaks

Core Data imports attributes (from type NSString) with spaces and new line


I am importing some data using NSXMLParser from XML into Core Data.

xml looks like:

<Translation>
                <LanguageCode>EN</LanguageCode>
                <SurahName>Al Anfal (The Spoils of War)</SurahName>
                <TranslatedText>Believers are…</TranslatedText>            
</Translation>

XML is ok i mean there aren't existing spaces there.

Then i want to display saved data on the App.

I concatenate different attributes into one string but it is not displayed in one line. (Integer values coming from other entity)

After debug, i realised that the attributes from type NSString added wrong into the core data. Namely they are containing spaces and line break.

I am using following code to concatenate string with integer values:

NSString *result = [NSString stringWithFormat:@"%@ - :%i / %i",currentTranslation.surahName,surahNr,verseNr];

Result should be a string without line breaks, but surahName pushes following integers to the next line.

Result:

Al Anfal (The Spoils of War)
                 - :8 / 2

As you see this part "- :8 / 2" printed in new line which pushed by surahName.

I searched this problem but i didn't find something. I don't know what i am doing wrong.

I hope the description above was clear.

Thank you in advance


Solution

  • The problem should be in the way you parse your XML. In the exemple you give, there is a new line caracter after the ending of each element.

    Here is the delegate's methods called to parse your surahname line:

    didStartElement (<SurahName>) 
    foundCharacters (Al Anfal (The Spoils of War))
    didEndElement (</SurahName>)
    foundCharacters (new line caracter)
    

    In foundCaracters method you have to check if those caracters are in an opened element, and if this element is supposed to contain caracters. If you determine caracters as usefull, add it to your current content, else don't use it.