4

In objectiveC given an NSArray fred containing strings I can say:

NSString *s = [fred ObjectAtIndex:5]; 

What is the c# equivalent? Rosetta stone says to use the method ValueAt but that returns an IntPtr.

var i = fred.ValueAt(5); 

But then you are left with how to convert an IntPtr to NSString pointer.

3 Answers 3

6

Going via IntPtr feels like a bad idea. Instead, as long as you know the type of the item you want to get, you can just use the following:

int index = 0; NSString str = myNsArr.GetItem<NSString>(index); 
Sign up to request clarification or add additional context in comments.

Comments

5

I've came to the same situation. You can convert that IntPtr to an NSString with new NSString(), like:

string strValue = new NSString(nsArr.ValueAt(1)); 

It's probably too late for you, but maybe it will help other people.

1 Comment

I tried your suggestion but I get the error "'Foundation.NSNumber.NSNumber(System.IntPtr)' is inaccessible due to it's protection level". Upon checking the Xamarin's bindings for NSNumber, I can see that it is protected: protected internal NSNumber (IntPtr handle);
1
string strValue = NSString.FromHandle(nsArr.ValueAt(1)); 

1 Comment

While this code may answer the question, it would be better to include some context, explain how it works, and describe when to use it. Code-only answers are not useful in the long run.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.