How to use NSArray in objective C. Card Deck on iPhone - objective-c

I´m all new to Objective C and I can´t seem to find how to do this (if it´s even possible).
I have a NSArray filled with 4 Cards (Card = my own Class) and I want to set the text of a label to a NSString which my card objects holds.
In Java I would do something like this:
If I have an Card Array with 4 Cards and theLabel is a String.
theLabel = deck[2].getLabel();
This don´t seem to work on Objective C. My code in Objective C so far:
- (IBAction)nextCard:(id)sender {
theLabel.text = [deck objectAtIndex:j].getLabel;
theImage.image = [deck objectAtIndex:j].getImage;
}
nextCard calls every time I click on a button on the iPhone.
j is an ordinary int that will keep track on which card to show.
It looks like this when I am creating the array:
- (void)viewDidLoad
{
[super viewDidLoad];
ah = [[Card alloc]init];
[ah setLabel:#"label1"];
[ah setCardImage: [UIImage imageNamed:#"3.png" ]];
as = [[Card alloc]init];
[as setLabel:#"label2"];
[as setCardImage: [UIImage imageNamed:#"2.png" ]];
ac = [[Card alloc]init];
[ac setLabel:#"labbel3"];
[ac setCardImage: [UIImage imageNamed:#"1.png" ]];
ad = [[Card alloc]init];
[ad setLabel:#"label4"];
[ad setCardImage: [UIImage imageNamed:#"4.png" ]];
deck = [NSMutableArray arrayWithCapacity:25];
[deck addObject:ah];
[deck addObject:as];
[deck addObject:ac];
[deck addObject:ad];
}
So I would basically want to be able to use methods ob objects stores in a NSArray.
Would really appreciate answers, thanks in advance!

In objective-C the generated getter hasn't the get prefix, so you use it that way:
theLabel = deck[2].label;
Or:
theLabel= [deck[2] label];

Check this:
[deck objectAtIndex:0].label;
or:
[[deck objectAtIndex:0] label];
or:
[[deck objectAtIndex:0] yourMethodWithParameter:#"foo"];

In Objective-C, in most cases the accessor methods are auto generated.
Let us say we have a property named "object"
The getter method is not named like this getObject , where "object" is the property you want to access.
It is named simply by the name of the property like this object
So in your case it should be something like this:
NSString *theLabelString = [[deck objectAtIndex:j] label];
//or this:
NSString *theLabelString = [deck[j] label];
//or this:
NSString *theLabelString = deck[j].label;

Related

PHPhotoLibrary getting album and photo info

I am trying to get info on all the albums/photos using the PHPhotoLibrary. I barely know objective C, and i've looked at some tutorial/sample but couldn't find everything that I needed.
Here is a link to the sample code I based my code on.
https://developer.apple.com/library/ios/samplecode/UsingPhotosFramework/Introduction/Intro.html#//apple_ref/doc/uid/TP40014575-Intro-DontLinkElementID_2
So far I was able to get the albums name and identifier. And I am getting a list of photos, I am able to get their identifier as well, but not the filename. But if I put a break point in my fonction and look at my PHAsset pointer values, I can see the filename there (inside _filename), but if I try to call the variable with the filename in it, the variable does not exist.
So if anyone can provide a sample code to get all info on albums/photos/thumbnail that would be awesome. Or just getting the filename would be a good help.
Here is the code I have tried so far:
-(void)awakeFromNib{
NSMutableArray *allPhotos = self.getAllPhotos;
for (int x = 0; x < allPhotos.count; x ++)
{
PHAsset *photo = [self getPhotoAtIndex:x];
PHAssetSourceType source = photo.sourceType;
NSString *id = photo.localIdentifier;
NSString *description = photo.description;
NSUInteger height = photo.pixelHeight;
NSUInteger width = photo.pixelWidth;
NSLog(#"Test photo info");
}
}
-(PHAsset*) getPhotoAtIndex:(NSInteger) index
{
return [self.getAllPhotos objectAtIndex:index];
}
-(NSMutableArray *) getAllPhotos
{
NSMutableArray *photos = [[NSMutableArray alloc] init];
PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
allPhotosOptions.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"creationDate" ascending:YES]];
PHFetchResult *allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
PHFetchResult *fetchResult = #[allPhotos][0];
for (int x = 0; x < fetchResult.count; x ++) {
PHAsset *asset = fetchResult[x];
photos[x] = asset;
}
return photos;
}
As you can see, I can get the image height and width, its id, but cannot get the url to it.
I have found a way to get the url of my photo.
-(void)getImageURL:(PHAsset*) asset
{
PHContentEditingInputRequestOptions *options = [[PHContentEditingInputRequestOptions alloc] init];
[options setCanHandleAdjustmentData:^BOOL(PHAdjustmentData *adjustmentData) {
return [adjustmentData.formatIdentifier isEqualToString:AdjustmentFormatIdentifier] && [adjustmentData.formatVersion isEqualToString:#"1.0"];
}];
[asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info)
{
NSURL* url = contentEditingInput.fullSizeImageURL;
}];
}
Filenames in the Photos library are an implementation detail and subject to change. There are various private API for discovering them (or ways to use valueForKey or other public introspection APIs to find where they're hidden), they aren't something to be relied upon. In particular, an asset that's been edited is likely to have a different filename than the original.
What do you need a filename/URL for? If you're just uniquely identifying the asset across launches of your app, use localIdentifier. If you're showing it to the user... why? Something like IMG_0234.jpg vs IMG_5672.jpg has little meaning to the average user.
To fetch the assets in a specific album, use fetchAssetsInAssetCollection:options:. To fetch the album(s) containing a specific asset, use fetchAssetCollectionsContainingAsset:withType:options:. To discover the list(s) of albums, use other APIs on PHAssetCollection and its superclass PHCollection.

(xcode objective-c) Array - finding a center of a object in an array

I'm pretty nooby when it comes to Xcode but here goes.
I have an NSArray with 5 UIImageView's, I have a script which will pick a random UIImageView, but I need it to then output the center of the image that was chosen.
My code looks like this:
NSMutableArray *Invader = [[NSMutableArray alloc] init];
[Invader addObject:H1];
[Invader addObject:H2];
[Invader addObject:H3];
[Invader addObject:H4];
[Invader addObject:H5];
Then like this to choose where to move a new UIImageView to:
if (FIRE1 == YES){
NSObject *Inv1 = [Invader randomObject]; //randomObject picks a random Invader
InvaderBullet1.hidden = NO;
InvaderBullet1.center = CGPointMake(Inv1.center.x, Inv1.center.y)
}
The error it's giving me is Property 'center' not found on object of type 'NSObject *'
I'm hoping this makes sense to anyone and that anyone can help me.
Thanks
NSObject obviously doesn't have a center property. Though you're not adding NSObjects to your array, you're adding (I assume) UIImageViews. So if you're adding UIImages, you can remove UIImages as well.
if (FIRE1 == YES){
NSObject *Inv1 = [Invader randomObject];
if ([Inv1 isKindOfClass:[UIImageView Class]]) //randomObject picks a random Invader
((UIImageView*)InvaderBullet1).hidden = NO;
((UIImageView*)InvaderBullet1).center = CGPointMake(Inv1.center.x, Inv1.center.y)
}
You simply need to specify the correct type for your variable:
if (FIRE1 == YES){
UIImageView *Inv1 = [Invader randomObject]; //randomObject picks a random Invader
InvaderBullet1.hidden = NO;
InvaderBullet1.center = CGPointMake(Inv1.center.x, Inv1.center.y)
}
This is assuming that your randomObject method (looks like a category method on NSArray) has a return type of id. If its return type is NSObject * then you need to do a cast:
UIImageView *Inv1 = (UIImageView *)[Invader randomObject]; //randomObject picks a random Invader
(Minor note: I recommend to stick to the established naming conventions. In this case: variables usually start with a lower-case character. You're doing other people that need/want to work with your code a favor.)

Predicate editor issue

Inside my custom scroll view i have added predicate object as under. It is giving error in the methods predicateWithSubpredicates in debug stack. Here is my sample code please let me know if any error are present.
-(void) awakeFromNib
{
NSPredicateEditor *predicateeditor = [[NSPredicateEditor alloc] initWithFrame:NSMakeRect(0, 0, 200, 150)];
NSArray *leftExpressions = [NSArray arrayWithObjects:[NSExpression expressionForKeyPath:#"date"], nil];
NSAttributeType rightType = NSDateAttributeType;
NSComparisonPredicateModifier modifier = NSAllPredicateModifier; //don't need "ANY" or "ALL"
NSArray *operators = [NSArray arrayWithObjects:[NSString stringWithString:#"Today"],[NSString stringWithString:#"Tomorrow"],[NSString stringWithString:#"Next week"], nil];
NSUInteger options = 0;
NSPredicateEditorRowTemplate *rowTemplate = [[NSPredicateEditorRowTemplate alloc] initWithLeftExpressions:leftExpressions rightExpressionAttributeType:rightType modifier:modifier operators:operators options:options];
[predicateeditor setRowTemplates:[NSArray arrayWithObject:rowTemplate]];
[rowTemplate release];
[self addSubview:predicateeditor];
[predicateeditor addRow:nil];
[predicateeditor displayValuesForRow:1];
[predicateeditor release];
}
As you've written it, this is going to define a predicate like this:
ALL date {"Today", "Tomorrow", "Next Week"} {a user-entered date}
I hope you can see that this is non-sensical.
For starters, if you really mean that //don't need "ANY" or "ALL", then you shouldn't be be using NSAllPredicateModifier. You should be using NSDirectPredicateModifier.
Also, the things that are allowed to go in the operators array are NSNumber objects that box one of the built-in predicate operator values.
So: what are you trying to accomplish?

Using a String representing the name of a variable to set the variable

This is a basic example that I know can be simplified, but for testing sake, I would like to do this in such a way. I want to set a variable based on an appending string (the variables "cam0" and "pos1" are already declared in the class). The appending string would essentially be an index, and i would iterate through a loop to assign cameras (cam0, cam1..) to different positions (pos0, pos1..).
cam0 is defined as an UIImageView
pos1 is defined as a CGRect
This works for a NSString Variable named coverIndex:
NSString* text = [[NSString alloc] initWithString:#""];
NSLog(#"%#",(NSString *)[self performSelector:NSSelectorFromString([text stringByAppendingString:#"coverIndex"])]);
The correct string that I set for coverIndex was logged to the Console.
Now back to my UIImageView and CGRect. I want this to work.
NSString* camText = [[NSString alloc] initWithString:#"cam"];
NSString* posText = [[NSString alloc] initWithString:#"pos"];
[(UIImageView *)[self performSelector:NSSelectorFromString([camText stringByAppendingString:#"0"])] setFrame:(CGRect)[self performSelector:NSSelectorFromString([posText stringByAppendingString:#"1"])]];
My error is "Conversion to non-scalar type requested"
This is the only way I found to do this sort of thing (and get the NSLog to work), but I still believe there is an easier way.
Thank you so much for any help :)
Use KVC, it's an amazing piece of technology that will do just what you want:
for (int index = 0; index < LIMIT; index++) {
NSString *posName = [NSString stringWithFormat:#"pos%d", index];
CGRect pos = [[self valueForKey:posName] CGRectValue];
NSString *camName = [NSString stringWithFormat:#"cam%d", index];
UIImageView *cam = [self valueForKey:camName];
cam.frame = pos;
}
One way you can do this would be to create your cameras in a dictionary and use those special NSStrings to key in to it. Like,
NSMutableDictionary *myCams;
myCams = [[myCams alloc] init];
[myCams addObject:YOUR_CAM0_OBJECT_HERE forKey:#"cam[0]"];
[myCams addObject:YOUR_CAM1_OBJECT_HERE forKey:#"cam[1]"];
NSString camString = #"cam[0]"; // you'd build your string here like you do now
id theCamYouWant = [myCams objectForKey:camString];

Add cell to an Array using data from a UIPickerView?

I have a UIPickerView with three components, and each component has NSIntegerMax for the numbers. So, I just need to get the data from each component, and send it to another ViewController called, CreationViewController. The three objects I need to send are strings, set up like so:
NSInteger supplyData = [supplypick selectedRowInComponent:0];
NSInteger mineralData = [supplypick selectedRowInComponent:1];
NSInteger vespeneData = [supplypick selectedRowInComponent:2];
So, I would like to add each cell in the format of this log:
NSLog(#"%i %# M:%i G:%i", supplyData, namer.text, mineralData, vespeneData);
All I need to know it what to put...
NSMutableArray * array = [[NSMutableArray alloc] init];
[array addObject: ];
^^HERE^^
This is all in one function. Please help! Thanks!
[array addObject:[NSString stringWithFormat:#"%i %# M:%i G:%i", supplyData, namer.text, mineralData, vespeneData]];