Converting NSArray Contents to a varargs (With ARC) For Use With NSString initWithFormat
Asked Answered
D

6

13

We have some code today that takes an NSArray and passes it as a argument list to -[NSString initWithFormat:arguments] and we're trying to get this to work with ARC. Here's the code were using

NSString* format = @"Item %s and Item %s"; // Retrieved elsewhere
NSArray* args = [NSArray arrayWithObjects:@"1", @"2", nil]; // Retrieved elsewhere

// http://cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html
char* argsList = (char*) malloc(sizeof(NSString*) * args.count);
[args getObjects:(id*) argsList];
NSString* message = [[[NSString alloc] initWithFormat:format arguments:argsList] autorelease];
free(argsList);

Any recommendations on how to make this ARC compliant? Or we're even open to a better way of doing it.

Dovecote answered 25/11, 2011 at 19:17 Comment(3)
https://mcmap.net/q/453868/-fake-va_list-in-arcConaway
I like that answer with NSMutableData, much "cleaner" (for a not very clean concept). Annoying how I couldn't find it with search.Dovecote
possible duplicate of How to create a NSString from a format string like @"xxx=%@, yyy=%@" and a NSArray of objects?Outrelief
L
4

This only works for arrays with a single element

The answer by chrisco was working well, until I went to compile with 64-bit architecture. This caused an error:

EXC_BAD_ADDRESS type EXC_I386_GPFLT

The solution was to use a slightly different approach for passing the argument list to the method:

+ (id)stringWithFormat:(NSString *)format array:(NSArray*) arguments;
{
     __unsafe_unretained id  * argList = (__unsafe_unretained id  *) calloc(1UL, sizeof(id) * arguments.count);
    for (NSInteger i = 0; i < arguments.count; i++) {
        argList[i] = arguments[i];
    }

    NSString* result = [[NSString alloc] initWithFormat:format, *argList] ;//  arguments:(void *) argList];
    free (argList);
    return result;
}
Leucocyte answered 28/1, 2014 at 23:54 Comment(9)
this one crash on iOS7Scholium
crash on [[NSString alloc] initWithFormat:format, *argList], nothing specific.Scholium
Marcin, without any debug output it is hard to say what is going on. But I might suggest that you are using something other than id to store the pointers (char or int perhaps?)Leucocyte
well... I did exact copy of code pasted in this answer. Crash every time with current Xcode5.Scholium
but what parameters you are calling it with? I imagine your format or array arguments are incorrect.Leucocyte
[ATLSAppDelegate stringWithFormat:@"test %@ - %@" array:@[@"hi", @"bye"]] Results in a EXC_BAD_ACCESS error.Gingivitis
I actually works for me, but just inserts the first parameter. Any fix?Lepido
I get EXC_BAD_ACCESS if my format string contains more than one item to be reformatted.Fugacious
This solution only works for a single element. Please consult #1059236Leucocyte
S
1

Cannot find a way to do this obj-c but a swift helper class finally got this working (my whole project is obj-c except this class)

@objc class StringFormat: NSObject {
    class func format(key: String, args: [AnyObject]) -> String {
        let locArgs: [CVarArgType] = args.map({ (arg: AnyObject) -> CVarArgType in
            if let iArg = (arg is NSNumber ? arg.intValue : nil) {
                return iArg
            }
            return arg as! CVarArgType
        });
        return String(format: key, arguments: locArgs)
    }
}

There is some magic going on, to do with how [CVarArgType] doesn't behave like a normal array - but this works in the flexible cross architecture way you expect it to.

Synchroflash answered 25/9, 2015 at 8:29 Comment(0)
T
1

Expanding on @mcfedr's answer, this Swift 3 helper does the job:

import Foundation

@objc (FTStringFormat) public class StringFormat: NSObject {
    @objc public class func format(key: String, args: [AnyObject]) -> String {
        let locArgs: [CVarArg] = args.flatMap({ (arg: AnyObject) -> CVarArg? in
            if let arg = arg as? NSNumber {
                return arg.intValue
            }
            if let arg = arg as? CustomStringConvertible {
                return arg.description
            }
            return nil
        });
        return String(format: key, arguments: locArgs)
    }
}

Calling from Objective-C:

[FTStringFormat formatWithKey:@"name: %@ age: %d" args:@[@"John", @(42)]]

For the %@ format specifier we're using Swift's CustomStringConvertible protocol in order to call description on all of the array members.

Supporting all number format specifiers like %d and %f is not really possible because the NSNumber object doesn't reveal if it's an integer or float. So we could only support one or the other. Here we use intValue, so %d is supported but %f and %g are not.

Tinct answered 9/2, 2017 at 19:8 Comment(0)
B
0

The only thing you need to do is remove the autorelease.

You're malloc'ing and free'ing yourself - ARC doesn't care about that.

Blueberry answered 25/11, 2011 at 19:50 Comment(1)
Unfortunately, ARC is would complain on the [args getObjects:argsList] which was the root of the problem.Dovecote
T
0

I write solution use NSInvocation and signatures.

Answer create in this. Also I write detailed description how it work but only on Russian ((

Maybe it help for someone.

Tobi answered 4/4, 2016 at 7:1 Comment(0)
C
0

I tried mcfedr's code. Somehow, my Xcode 11 treated CVarArgType as undeclared type, so I investigated into this for a while.

I didn't not understand the closure part of his/her code. And, I just simplified to hard casted each element to CVarArg using as! operator.

func format(key: String, args: [Any]) -> String {
    return String(format: key, arguments: args.map { ($0 as! CVarArg) })
}

let doubleValue: Double = 1.25
let floatValue: Float = 2.75
let intValue: Int = 3
let numberValue: NSNumber = 4.5 as NSNumber
let hello: String = "Hello"
let world: NSString = "World" as NSString
print(format(key: "double: %f, float: %f, int: %d, number: %@, %@, %@", args: [doubleValue, floatValue, intValue, numberValue, hello, world]))

// double: 1.250000, float: 2.750000, int: 3, number: 4.5, Hello, World

It seems it's working fine under swift 5.1, but there may be some pitfalls.

Cuisine answered 28/9, 2019 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.