Weird problem with writeToUrl:ofType:error

  • I'm implementing writeToUrl:ofType:error in my document to save some
    data as a simple text file. This document also serves as a datasource
    for a TableView. Basically just an NSMutableArray with some simple
    objects in it (that are displayed in the TableView)....I can add/delete
    object to the array and these objects are then displayed in the
    NSTableView... that part is working well...

    When saving the document (i.e. in writeToUrl) what I am doing is rather
    simple..just looping through the NSMutableArray, and formatting each
    object to a string (that part isn't a problem..I added a "toString"
    method to my objects...). I append that string to a MutableString that I
    will then save to a text file once the loop is done...

    So far, everything seems to be working... the only problem is that... in
    the writeToUrl method, my NSMutableArray's count is always 1.... it only
    contains the first object (the one which I add on initialization so the
    tableview isn't empty)... any new objects that were added to the
    NSMutableArray aren't there...

    What's even weirder is that I added a bogus button that will display my
    NSMutableArray's count in the log whenever I click on it.... and even
    there, I get the correct count... (for example:  Current Array Count:
    10).... Then I save the document...and in writeToUrl, I re-print the
    count in the log and I get:  Current Array Count: 1.... and only the
    initial object is being saved in the file....

    It seems as if the writeToUrl method was using a different Array...I
    don't see how it would be possible? My NSMutableArray is an instance
    variable of my Document class... the Array's count seems to be correct
    at any given moment....except when saving (in writeToUrl...)...

    Any help would be appreciated!
  • On 11/15/07, Jean-Nicolas Jolivet <silvertab...> wrote:

    > Any help would be appreciated!

    It sure sounds like you are using different array instances. Use the
    debugger to view address of the array when you modify/access it to
    understand if and how that is happening.

    Don't think we can help much without a sample of code (ideally enough
    to reproduce the problem).

    -Shawn
  • What's weird is that: I only have 1 instance of the NSMutableArray and
    it is the Instance Variable...

    I'm using this code to print the Array count when a button is clicked
    (subtitles is the name of my instance variable)

    - (IBAction)debugPressed:(id)sender {
        NSLog(@"Debug Count: %d", [subtitles count]);
    }

    and I'm using this code to print the count when saving to file (in
    writeToUrl):
    NSLog(@"Count when saving: %d", [subtitles count]);

    Both methods are in the MyDocument class which has the subtitles
    NSMutableArray...

    I just don't see how one can use a different instance of the array :(

    Shawn Erickson wrote:
    > On 11/15/07, Jean-Nicolas Jolivet <silvertab...> wrote:
    >
    >
    >> Any help would be appreciated!
    >>
    >
    > It sure sounds like you are using different array instances. Use the
    > debugger to view address of the array when you modify/access it to
    > understand if and how that is happening.
    >
    > Don't think we can help much without a sample of code (ideally enough
    > to reproduce the problem).
    >
    > -Shawn
    >
    >
  • Using the following:
    NSLog(@"Debug Count: %d, Object: %@", [subtitles count], subtitles);
    Both when saving, and when adding/deleting stuff from the array... I was
    able to see that the addresses of the objects in my array are
    different...so it really is a different instance... Now the thing is..
    how could this happen!.. My code is pretty simple and straightforward
    and should all be using the same instance variable (the Mutable Array)
    that belongs to the MyDocument class... it's as if a different instance
    of MyDocument was actually doing the saving... how is this possible?

    Shawn Erickson wrote:
    > On 11/15/07, Jean-Nicolas Jolivet <silvertab...> wrote:
    >
    >
    >> Any help would be appreciated!
    >>
    >
    > It sure sounds like you are using different array instances. Use the
    > debugger to view address of the array when you modify/access it to
    > understand if and how that is happening.
    >
    > Don't think we can help much without a sample of code (ideally enough
    > to reproduce the problem).
    >
    > -Shawn
    >
    >
  • Something strange I just noticed...when I launch my application, 2
    instances of MyDocument are created... (I've used NSLog(@"MyDocument
    init")  in MyDocument's init method..)

    Is this normal? Because it would definitely explain my problem... A
    different instance of MyDocument is being saved somehow... now idea how
    it could've happened...

    Jean-Nicolas Jolivet wrote:
    > Using the following:
    > NSLog(@"Debug Count: %d, Object: %@", [subtitles count], subtitles);
    > Both when saving, and when adding/deleting stuff from the array... I
    > was able to see that the addresses of the objects in my array are
    > different...so it really is a different instance... Now the thing is..
    > how could this happen!.. My code is pretty simple and straightforward
    > and should all be using the same instance variable (the Mutable Array)
    > that belongs to the MyDocument class... it's as if a different
    > instance of MyDocument was actually doing the saving... how is this
    > possible?
    >
    >
    > Shawn Erickson wrote:
    >> On 11/15/07, Jean-Nicolas Jolivet <silvertab...> wrote:
    >>
    >>
    >>> Any help would be appreciated!
    >>>
    >>
    >> It sure sounds like you are using different array instances. Use the
    >> debugger to view address of the array when you modify/access it to
    >> understand if and how that is happening.
    >>
    >> Don't think we can help much without a sample of code (ideally enough
    >> to reproduce the problem).
    >>
    >> -Shawn
    >>
    >>

    >
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today