Runtime info of NSObjects in an NSArray

mannyvwmannyvw USMember

Hi

Have trouble getting objects out of NSArray that is return from a native libary.

So the ObjC interface is

/**

 Gets all items from the current shopping trip.

 @param completion The block to execute when the method is finished. This block has no return value and takes one argument: An `NSArray` containing objects that conform to the <ShoppingItemProtocol>, or `nil` if the item does not exist.

 */
- (void)getItemsFromShoppingTrip:(void (^)(NSArray *shoppingItems))completion;

which i bind as.

void GetItemsFromShoppingTrip (Action<NSArray> completion);

Making the call is fine and a get back the NSArray, but when i try and get the objects out of the NSArray which are defined in objc as

@protocol ShoppingItemProtocol

/// The unique ID of this shopping item.

- (NSString *)guid;

..... etc

@end

they ended up like this in the binding file

[BaseType (typeof (NSObject))]

public partial interface ShoppingItemProtocol {

    [Export ("guid")]
    string Guid { get; }  

..... etc

}

this is my code to get the object

       private void ShoppingListLoaded(NSArray items)
            {
                Console.WriteLine("ShoppingListLoaded " + items.Count);

                for (uint i = 0; i < items.Count;i++)
                {
                    NSObject a = MonoTouch.ObjCRuntime.Runtime.GetNSObject(items.ValueAt(i));
                    Console.WriteLine(a.Description);
                    ShoppingItemProtocol item = a as ShoppingItemProtocol;

                }
            }  

So variable a is populated fine, but when i try and cast a to the actual object of ShoppingItemProtocol it becomes NULL, so it seems to have lost the runtime info ? interestingly all the properties are added to the .Description property as a comma delimited string, so i can get at the data, but in an inelegant way, any ideas what i can do to get at the properly object ?

thanks

Posts

  • RolfBjarneKvingeRolfBjarneKvinge USXamarin Team Xamurai

    Try to bind like this:

    [BaseType (typeof (NSObject))]
    [Protocol]
    public partial interface ShoppingItemProtocol {
        /* ... */
    }
    
    interface IShoppingItemProtocol {}
    
    [BaseType (typeof (?))]
    public partial interface MyUnknownClass {
        void GetItemsFromShoppingTrip (Action<IShoppingItemProtocol []> completion);
    }
    
  • mannyvwmannyvw USMember

    Thanks. Is there a bit missing ? When I iterate through objects using IShoppingItemProtocol none of the ShoppingItemProtocol properties are there, I can't see the relationship between ShoppingItemProtocol & IShoppingItemProtocol defined anywhere in your example

    N

  • mannyvwmannyvw USMember

    Ah don't worry worked it out you just meant to use same name for both. :-) thanks working now.

Sign In or Register to comment.