0
NSUInteger arrayLength = [annotations count];

    UILabel* arrayLengthLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
    arrayLengthLabel.text = @"%@", arrayLength;
    [self.view addSubview:arrayLengthLabel];

I have the above code, which I have researched.

The problem is when outputting the arrays length count to the label.

I should have 4 elements in the array, but this is not outputting the number of elements, and I am sure I am making a schoolboy error here.

Cheers for help in advance

3 Answers 3

1

Here is the code I would use:

int arrayLength = [annotations count];
UILabel* arrayLengthLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
arrayLengthLabel.text = [NSString stringWithFormat:@"%i",arrayLength];
[self.view addSubview:arrayLengthLabel];

I'm using a standard int and using the code %i instead of %@. This may be your issue. I don't know offhand if an NSUInteger requires %i, but I definitely know int uses %i.

Also, I don't know if your way of doing an NSString works without testing, so I've used the "full" format, which I know definitely works.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do it that way :

UILabel* myLabel = [[UILabel alloc] initWithFrame: CGRectMake(25, 100, 110, 100)];
myLabel = [NSString stringWithFormat: @"%i", [myArray count]];
[self.view addSubview: myLabel];

2 Comments

NSArray's count method outputs a NSUInteger which is just a 32/64bit unsigned integer and I don't believe it responds to stringValue developer.apple.com/library/mac/documentation/Cocoa/Reference/… I think you're thinking of NSNumber
Oops, you're right. My mistake. I'll change that, even though the other answers use this too.
0

Make the changes as follows:

NSUInteger arrayLength = [annotations count];
UILabel* arrayLengthLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
arrayLengthLabel.text = [NSString stringWithFormat:@"%d", arrayLength];//changes made here
[self.view addSubview:arrayLengthLabel];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.