Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
When my program is not able to move a file to a different location I add the file name to an NSMutableArray. When the program finishes, and the array has a count of more the 0, I have an NSAlert display on the screen with a list of files that did not transfer. It works just fine, or as expected, but I get this warning. The part of the alert that takes the argument takes an NSString. I created an NSString but I get the error.


Code:
-(void)transferAlertError{
    NSString *myArrayString = [[canNotTransferArray valueForKey:@"description"]  componentsJoinedByString:@", "];
    
    NSAlert *alert = [NSAlert alertWithMessageText:@"Files not copied because of duplicate names." defaultButton:@"Ok" alternateButton:nil otherButton:nil informativeTextWithFormat:myArrayString];
    [alert setAlertStyle:NSInformationalAlertStyle];
    [alert runModal];
}

I did a Google search and found a number of these problems to figure it out first. I did try to use [NSString stringWithFormat:mad:"%@,] but it still gave me the same problem. I'm pretty sure it is a formatting issue but I just can't put my finger on it.

Any help at this point would be appreciated.
 
Your first line confuses me... You're getting the description of an array, which is a string. Then you're trying to join components together, but that doesn't make sense because you have a string at that point, not an array.

Can you give more details on your error? Is it occurring at runtime or compile time? Which line is the error occurring on?
 
componentsJoinedByString is an instance method of NSArray. Is the object returned by canNotTransferArray valueForKey:mad:"description" an instance of NSArray?

If not then componentsJoinedByString will return nil, probably not what you expected.
 
The "informativeTextWithFormat:" argument expects a literal format string. The method then accepts variable arguments for the parameters that fill in placeholders. You're generating a formatted string and feeding it in. If you inadvertently passed format specifiers you'd have problems. The format @"%@" and your current string following it should do it.

-Lee
 
I did a Google search and found a number of these problems to figure it out first. I did try to use [NSString stringWithFormat:mad:"%@,] but it still gave me the same problem. I'm pretty sure it is a formatting issue but I just can't put my finger on it.

The last parameter is a format string, followed by optional parameters. Similar to printf. For example it could be:

informativeTextWithFormat:mad:"x = %d, y = %d", x, y

So the format string should be a string literal, to allow the compiler to check it. In this example, the compiler will see two %d in the string and check that there are two int values.

If it isn't a string literal, then you are in danger - if the string contains for example %d, then it will want an integer value to substitute for the %d, which most likely will produce rubbish. If the string contains %s or %@, then your program will most likely crash.

Passing in [NSString stringWithFormat... ] just gives the same problem again.

Pass as the last parameter:

informativeTextWithFormat:mad:"%@", myArrayString
 
Thanks guys, you pointed out something I didn't even notice. I was so focused on that it was an NSString and that was the parameter that I was providing.

informativeTextWithFormat:

I then 'READ THE DOCS' completely and the keyword "Format" jumped out at me, as in Format specifier. Which then the docs had a link, "For more information on format strings, see “Formatting String Objects”." and the examples you had made sense.

I revised my Method to this and now it works as expected with errors.

Code:
-(void)transferAlertError{
    NSString *myArrayString = [canNotTransferArray description];
    
    NSAlert *alert = [NSAlert alertWithMessageText:@"Files not copied because of duplicate names." defaultButton:@"Ok" alternateButton:nil otherButton:nil informativeTextWithFormat:@"%@", myArrayString];
    [alert setAlertStyle:NSInformationalAlertStyle];
    [alert runModal];
}

Thanks for the help folks....
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.