简体   繁体   中英

Passing an unknown type (?) object to a function as a parameter in objective-c

How do I pass an unknown type (?) object to a function and then set it to a local ones value in objective-c? The unknown object is an n-th item of a JSON value. I use the JSON framework.

[giveMeTheNthObj:[items objectAtIndex:indexPath.row]]
...
- (void)giveMeTheNthObj:(id)o { 
    Classname *local = o;
}

The above fails. I think because the local is a pointer that should point to a Classname type. What should be the type of the local variable?

The error log:

GNU gdb 6.3.50-20050815 (Apple version gdb-1705) (Fri Jul  1 10:50:06 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".Attaching to process 37438.
2011-09-30 16:39:21.649 Navig[37438:b303] -[UILabel copyWithZone:]: unrecognized 
selector sent to instance 0x4b949a0
2011-09-30 16:39:21.652 Navig[37438:b303] *** Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: '-[UILabel copyWithZone:]: unrecognized selector 
sent to instance 0x4b949a0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00dde5a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00f32313 objc_exception_throw + 44
    2   CoreFoundation                      0x00de00bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00d4f966 ___forwarding___ + 966
    4   CoreFoundation                      0x00d4f522 _CF_forwarding_prep_0 + 50
    5   CoreFoundation                      0x00d3edca -[NSObject(NSObject) copy] + 42
    6   UIKit                               0x000de18b -[UIViewController setTitle:]  95
    7   Foundation                          0x007b05e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
    8   UIKit                               0x0022c30c -[UIRuntimeOutletConnection connect] + 112
    9   CoreFoundation                      0x00d548cf -[NSArray makeObjectsPerformSelector:] + 239
    10  UIKit                               0x0022ad23 -[UINib instantiateWithOwner:options:] + 1041
    11  UIKit                               0x0022cab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
    12  UIKit                               0x000e2628 -[UIViewController _loadViewFromNibNamed:bundle:] + 70
    13  UIKit                               0x000e0134 -[UIViewController loadView] + 120
    14  UIKit                               0x000e000e -[UIViewController view] + 56
    15  UIKit                               0x000de482 -[UIViewController contentScrollView] + 42
    16  UIKit                               0x000eef25 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 48
    17  UIKit                               0x000ed555 -[UINavigationController _layoutViewController:] + 43
    18  UIKit                               0x000ee7aa -[UINavigationController _startTransition:fromViewController:toViewController:] + 326
    19  UIKit                               0x000e932a -[UINavigationController _startDeferredTransitionIfNeeded] + 266
    20  UIKit                               0x000f0562 -[UINavigationController pushViewController:transition:forceImmediate:] + 932
    21  UIKit                               0x089b9c36 [UINavigationControllerAccessibility(SafeCategory) pushViewController:transition:forceImmediate:] + 77
    22  UIKit                               0x000e91c4 -[UINavigationController pushViewController:animated:] + 62
    23  Navig                               0x00002dfd -[RootViewController tableView:didSelectRowAtIndexPath:] + 269
    24  UIKit                               0x000a7b68 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140
    25  UIKit                               0x0009db05 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
    26  Foundation                          0x007b779e __NSFireDelayedPerform + 441
    27  CoreFoundation                      0x00dbf8c3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
    28  CoreFoundation                      0x00dc0e74 __CFRunLoopDoTimer + 1220
    29  CoreFoundation                      0x00d1d2c9 __CFRunLoopRun + 1817
    30  CoreFoundation                      0x00d1c840 CFRunLoopRunSpecific + 208
    31  CoreFoundation                      0x00d1c761 CFRunLoopRunInMode + 97
    32  GraphicsServices                    0x010161c4 GSEventRunModal + 217
    33  GraphicsServices                    0x01016289 GSEventRun + 115
    34  UIKit                               0x0003ec93 UIApplicationMain + 1160
    35  Navig                               0x00002439 main + 121
    36  Navig                               0x000023b5 start + 53
)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
Current language:  auto; currently objective-c
(gdb) 

When you say "The above fails."; how exactly does it fail? Exception raised at run-time, warning during compile, etc? Does this help:

- (void)giveMeTheNthObj:(id)o {
    if ([o isKindOfClass:[Classname class]]) {
        Classname *local = (Classname *)o;
    }
}

You want NSString *local = NSStringFromClass([o class]); ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM