Resolved after parsing an XML file is there any way to edit the string you get back?

Discussion in 'iPhone/iPad Programming' started by webznz, Apr 7, 2011.

  1. webznz, Apr 7, 2011
    Last edited: Apr 7, 2011

    macrumors member

    Joined:
    Mar 8, 2011
    Messages:
    82
    Location:
    Hobbitin
    #1
    I am currently connecting to this RSS feed that I am loading the title fields in the xml to my UItableView. however in the title field each title begins with the word "Preview:" is there any way to omit this word from each of the strings I get back?
     
  2. Moderator

    robbieduncan

    Staff Member

    Joined:
    Jul 24, 2002
    Messages:
    24,480
    Location:
    London
    #2
    If you have an NSString you can use any of the documented methods. So yes you can edit them (obviously NSString is immutable so you actually create a new object but the effect is the same).
     
  3. macrumors member

    Joined:
    Mar 8, 2011
    Messages:
    82
    Location:
    Hobbitin
    #3
    Cool, so what I am doing is copying my NSString variable into a NSMutableString temp variable then performing this function I have found

    Code:
    [tempString deleteCharactersInRange: [tempString rangeOfString: @"Preview:"]];
    however this dose not work... which is a surprise however I know why. its because of the : at the end of Preview... it works when I remove that from the string but dosn't work when its included. Just wondering How I might get around this? or if there is some special specifier I can put before the : to identify it like you do with "" in C++
     
  4. Moderator

    robbieduncan

    Staff Member

    Joined:
    Jul 24, 2002
    Messages:
    24,480
    Location:
    London
    #4
    : is not a special character inside NSStrings. You don't need to escape it. Are you sure that there is no a space between Preview and : or any other issue?
     
  5. webznz, Apr 7, 2011
    Last edited by a moderator: Apr 7, 2011

    macrumors member

    Joined:
    Mar 8, 2011
    Messages:
    82
    Location:
    Hobbitin
    #5
    Positive here is my NSLog before and after the deleteCharactersInRange: function with "Preview" as the selection I am looking for. If I include the : in the selection I'm looking for the app crashes.

    Code:
    2011-04-08 10:15:10.861 TopSongs[1664:207] Before= Preview: Blue Estate
    2011-04-08 10:15:10.862 TopSongs[1664:207] After= : Blue Estate
    
    EDIT:

    actually I just tried running with the : included and looked at my NS log.. I think I know what might be the problem.

    here is the log.
    Code:
    [Session started at 2011-04-08 10:20:04 +1200.]
    2011-04-08 10:20:06.067 TopSongs[1747:207] Before= CBR Previews
    2011-04-08 10:20:06.069 TopSongs[1747:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFString deleteCharactersInRange:]: Range or index out of bounds'
    *** Call stack at first throw:
    (
    	0   CoreFoundation                      0x00dafbe9 __exceptionPreprocess + 185
    	1   libobjc.A.dylib                     0x00f045c2 objc_exception_throw + 47
    	2   CoreFoundation                      0x00d68628 +[NSException raise:format:arguments:] + 136
    	3   CoreFoundation                      0x00d6859a +[NSException raise:format:] + 58
    	4   Foundation                          0x000a5444 mutateError + 218
    	5   TopSongs                            0x00002a23 -[RSSTableViewController parser:didEndElement:namespaceURI:qualifiedName:] + 220
    	6   Foundation                          0x000f1a19 _endElementNs + 453
    	7   libxml2.2.dylib                     0x01241e63 xmlParseXMLDecl + 1346
    	8   libxml2.2.dylib                     0x0124cb6d xmlParseChunk + 3984
    	9   Foundation                          0x000f121a -[NSXMLParser parse] + 321
    	10  TopSongs                            0x000027ea -[RSSTableViewController connectionDidFinishLoading:] + 145
    	11  Foundation                          0x0005a172 -[NSURLConnection(NSURLConnectionReallyInternal) sendDidFinishLoading] + 108
    	12  Foundation                          0x0005a0cb _NSURLConnectionDidFinishLoading + 133
    	13  CFNetwork                           0x0137a606 _ZN19URLConnectionClient23_clientDidFinishLoadingEPNS_26ClientConnectionEventQueueE + 220
    	14  CFNetwork                           0x01445821 _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 293
    	15  CFNetwork                           0x01370e3c _ZN19URLConnectionClient13processEventsEv + 100
    	16  CFNetwork                           0x01370cb7 _ZN17MultiplexerSource7performEv + 251
    	17  CoreFoundation                      0x00d9101f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    	18  CoreFoundation                      0x00cef28b __CFRunLoopDoSources0 + 571
    	19  CoreFoundation                      0x00cee786 __CFRunLoopRun + 470
    	20  CoreFoundation                      0x00cee240 CFRunLoopRunSpecific + 208
    	21  CoreFoundation                      0x00cee161 CFRunLoopRunInMode + 97
    	22  GraphicsServices                    0x016e4268 GSEventRunModal + 217
    	23  GraphicsServices                    0x016e432d GSEventRun + 115
    	24  UIKit                               0x002c742e UIApplicationMain + 1160
    	25  TopSongs                            0x00002260 main + 102
    	26  TopSongs                            0x000021f1 start + 53
    )
    terminate called after throwing an instance of 'NSException'
    as you can see there is a <title>CBR Previews</title> which doesn't include the Preview: string. I need to figure out how to make an exception for this...

    what do you think, think its the problem?
     
  6. Moderator

    robbieduncan

    Staff Member

    Joined:
    Jul 24, 2002
    Messages:
    24,480
    Location:
    London
    #6
    deleteCharactersInRange: will, quite reasonably, raise an exception if you pass it an invalid range. So don't pass it an invalid range.

    Split the call into to and wrap the deleteCharactersInRange: in a condition where you check the NSRange is a valid range in the string.
     
  7. macrumors member

    Joined:
    Mar 8, 2011
    Messages:
    82
    Location:
    Hobbitin
    #7
    cool thanks, yea Was just checking to see how NSRange worked.. glad to see it'll be the solution.

    thanks for the help :)
     

Share This Page