NSMutableArray to NSArray?

  • Is there a way to create a NSArray from a NSMutableArray? I tried
    with methods -writeToFile and -initWithContentsOfFile but I've got a
    problem with the encoding of the contents. Better If I explain what
    I'm trying to do. I have a NSMutableArray full of NSStrings objects,
    in my case of paths to files, and I want to put all these content in
    a NSTableView, but the problem is, that NSTableView only accepts
    NSArray for dataSource. What can I do to solve my problem?

    About the problems I talk before, I've tried with diferent paths, and
    the -writeToFile works fine, but when i execute the -
    initWithContentsOfFile the application crash. I had been looking what
    was wrong and I discovered that the problem is that
    initWithContentsOfFile can't read paths with characters in the
    NSString like " ¨ " and others.

    Thanks
  • At 7:22 PM +0200 10/4/07, Dani wrote:

    > Is there a way to create a NSArray from a NSMutableArray?

    NSArray *newArray = [NSArray arrayWithArray:myMutableArray];

    --
    randy
  • On Thu, October 4, 2007 1:22 pm, Dani said:
    > Is there a way to create a NSArray from a NSMutableArray? I tried
    > with methods -writeToFile and -initWithContentsOfFile but I've got a
    > problem with the encoding of the contents. Better If I explain what
    > I'm trying to do. I have a NSMutableArray full of NSStrings objects,
    > in my case of paths to files, and I want to put all these content in
    > a NSTableView, but the problem is, that NSTableView only accepts
    > NSArray for dataSource. What can I do to solve my problem?

    NSMutableArray can be used as an NSArray whenever the latter is called
    for: That's one of the results of it being an NSArray subclass.

    Daniel T. Staal

    ---------------------------------------------------------------
    This email copyright the author.  Unless otherwise noted, you
    are expressly allowed to retransmit, quote, or otherwise use
    the contents for non-commercial purposes.  This copyright will
    expire 5 years after the author's death, or in 30 years,
    whichever is longer, unless such a period is in excess of
    local copyright law.
    ---------------------------------------------------------------
  • Thanks for your response. I will take a look.

    El 04/10/2007, a las 19:28, Benjamin Stiglitz escribió:

    >> Is there a way to create a NSArray from a NSMutableArray? I tried
    >> with methods -writeToFile and -initWithContentsOfFile but I've got
    >> a problem with the encoding of the contents. Better If I explain
    >> what I'm trying to do. I have a NSMutableArray full of NSStrings
    >> objects, in my case of paths to files, and I want to put all these
    >> content in a NSTableView, but the problem is, that NSTableView
    >> only accepts NSArray for dataSource. What can I do to solve my
    >> problem?
    >
    > You need to use either an NSArrayController or a custom table view
    > data source. Check out the NSArrayController documentation.
    >
    > -Ben
  • NSMutableArray *is* an NSArray, there is no need to convert. Your
    problem likely lies elsewhere.

    On 10/4/07, Dani <daniel.amselem...> wrote:
    > Is there a way to create a NSArray from a NSMutableArray? I tried
    > with methods -writeToFile and -initWithContentsOfFile but I've got a
    > problem with the encoding of the contents. Better If I explain what
    > I'm trying to do. I have a NSMutableArray full of NSStrings objects,
    > in my case of paths to files, and I want to put all these content in
    > a NSTableView, but the problem is, that NSTableView only accepts
    > NSArray for dataSource. What can I do to solve my problem?
    >
    > About the problems I talk before, I've tried with diferent paths, and
    > the -writeToFile works fine, but when i execute the -
    > initWithContentsOfFile the application crash. I had been looking what
    > was wrong and I discovered that the problem is that
    > initWithContentsOfFile can't read paths with characters in the
    > NSString like " ¨ " and others.
    >
    > Thanks
    >

    --
    Clark S. Cox III
    <clarkcox3...>
  • To expand on what Clark and Daniel said, this is one of the core
    principles of object oriented programming - polymorphism. See http://
    en.wikipedia.org/wiki/Object-oriented_programming, etc..

    Even though it's not likely to solve your problem, you can create an
    NSArray from an NSMutableArray with any of the following:
    - copy
    -[NSArray initWithArray:]
    +[NSArray arrayWithArray:]

    These will create a new NSArray whose contents are the same as the
    NSMutableArray. (Normal memory management rules apply)

    HTH,
    Glen

    On Oct 4, 2007, at 12:31 PM, Clark Cox wrote:

    > NSMutableArray *is* an NSArray, there is no need to convert. Your
    > problem likely lies elsewhere.
    >
    > On 10/4/07, Dani <daniel.amselem...> wrote:
    >> Is there a way to create a NSArray from a NSMutableArray? I tried
    >> with methods -writeToFile and -initWithContentsOfFile but I've got a
    >> problem with the encoding of the contents. Better If I explain what
    >> I'm trying to do. I have a NSMutableArray full of NSStrings objects,
    >> in my case of paths to files, and I want to put all these content in
    >> a NSTableView, but the problem is, that NSTableView only accepts
    >> NSArray for dataSource. What can I do to solve my problem?
    >>
    >> About the problems I talk before, I've tried with diferent paths, and
    >> the -writeToFile works fine, but when i execute the -
    >> initWithContentsOfFile the application crash. I had been looking what
    >> was wrong and I discovered that the problem is that
    >> initWithContentsOfFile can't read paths with characters in the
    >> NSString like " ¨ " and others.
    >>
    >> Thanks
    >>
    >
    >
    > --
    > Clark S. Cox III
    > <clarkcox3...>
previous month october 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 31        
Go to today