Re: Variable Number of Parameters in Method
Re: Variable Number of Parameters in Method
- Subject: Re: Variable Number of Parameters in Method
- From: Maxthon Chan <email@hidden>
- Date: Thu, 22 Aug 2013 15:19:37 +0800
This is actually an ABI issue. Objective-C method calls are essentially C function calls:
[NSArray arrayWithObjects:@1, @2, @3, nil];
is really (C function symbol name is guessed from a common name mangling scheme)
__objc_c_NSArray_arrayWithObjects_([NSArray class], @selector(arrayWithObjects:), @1, @2, @3, nil); // Prototype: id imp(id self, SEL _cmd, id, …);
So the problem is how the parameter is passed from the caller function to the callee. I am not so familiar with ARM so I am going to use amd64 as an example:
amd64 sysv ABI passes initial six integer and pointer parameters in registers rsi, rdi, r8, r9, r10, r11, r12, r13, r14 and remainders on stack. Float point parameters is, instead, first in SSE registers and then stack. That is why floats will cause issue.
On Aug 22, 2013, at 14:37, Dave <email@hidden> wrote:
> Hi,
>
> It's not much good to me since I don't have a problem passing objects but with passing a mixed set.
>
> Type passing, 1,2,3,5.6,@"hello",nil to the arrayWithObjects and you will see what I mean
>
> Cheers
> Dave
>
>
> On 21 Aug 2013, at 20:04, Alex Zavatone <email@hidden> wrote:
>
>> Just in case the styled text is stripped, here is the line I'm referring to from the header file:
>>
>> + (id)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
>>
>> Sent from my iPad
>>
>> On Aug 21, 2013, at 2:58 PM, Alex Zavatone <email@hidden> wrote:
>>
>>> The approach I mentioned is what NSArray uses to allow an arbitrary amount of objects in this call to init it. Supplying nil tells the runtime that there are no more parameters.
>>>
>>> NSArray *myArray;
>>> myArray = [NSArray alloc] initWithObjects: @"my String", @"another", @"more", nil];
>>>
>>> It's the nil termination of the parameters approach.
>>>
>>> How you do this is that bold line that I outlined earlier.
>>>
>>> Sent from my iPad
>>>
>>> On Aug 21, 2013, at 1:17 PM, "Stephen J. Butler" <email@hidden> wrote:
>>>
>>>> On Wed, Aug 21, 2013 at 4:22 AM, Dave <email@hidden> wrote:
>>>>
>>>>> if ([myType isEqualToString:@"NSInteger"] )
>>>>> {
>>>>> myNSInteger = va_arg(myArgumentList,NSInteger*);
>>>>> // do something with myNSInteger
>>>>> [myFormattedString appendFormat:@"myNSInteger = %d ",
>>>>> myNSInteger];
>>>>> }
>>>>>
>>>>> else if ([myType isEqualToString:@"CGFloat"] )
>>>>> {
>>>>> myCGFloat = va_arg(myArgumentList, CGFloat*);
>>>>> // do something with myCGFloat
>>>>> [myFormattedString appendFormat:@"myCGFloat = %f ",
>>>>> myCGFloat];
>>>>> }
>>>>
>>>>
>>>> This is better, however... if you pass an actual NSInteger or CGFloat to
>>>> the method, va_arg() should have a type of NSInteger or CGFloat, not
>>>> pointers to those types. Your code examples with it's printf style formats
>>>> is using the former, not the latter. Just to be clear, you can have all
>>>> these cases:
>>>>
>>>> if ([myType isEqualToString:@"NSInteger"] ) {
>>>> myNSInteger = va_arg(myArgumentList,NSInteger);
>>>> } else if ([myType isEqualToString:@"NSInteger*"] ) {
>>>> pMyNSInteger = va_arg(myArgumentList,NSInteger*);
>>>> } else if ([myType isEqualToString:@"CGFloat"]) {
>>>> myCGFloat = va_arg(myArgumentList, CGFloat);
>>>> } else if ([myType isEqualToString:@"CGFloat*"]) {
>>>> pMyCGFloat = va_arg(myArgumentList, CGFloat*);
>>>> } ... etc
>>>> _______________________________________________
>>>>
>>>> Cocoa-dev mailing list (email@hidden)
>>>>
>>>> Please do not post admin requests or moderator comments to the list.
>>>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>>>
>>>> Help/Unsubscribe/Update your Subscription:
>>>>
>>>> This email sent to email@hidden
>>>
>>> _______________________________________________
>>>
>>> Cocoa-dev mailing list (email@hidden)
>>>
>>> Please do not post admin requests or moderator comments to the list.
>>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>>
>>> Help/Unsubscribe/Update your Subscription:
>>>
>>> This email sent to email@hidden
>>
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden