I`m making an exercise, i have 2 classes Songs and Playlist, in the program i can put songs in playlist and everything is working fine, but i need to make a master playlist with all the songs that are in all the normal playlist and there is the problem.
Here is the code
//
// main.m
// MyItunes
//
// Created by Rodrigo López on 6/29/12.
// Copyright (c) 2012 ITQ. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Songs.h"
#import "PlayList.h"
int main (int argc, const char * argv[])
{
#autoreleasepool {
Songs *mySong1 = [Songs new];
Songs *mySong2 = [Songs new];
Songs *mySong3 = [Songs new];
PlayList *myPlayList1=[[PlayList alloc]initWithName:#"First"];
PlayList *myPlayList2=[[PlayList alloc]initWithName:#"Second"];
[mySong1 setTitle:#"Back In Black" setArtist:#"AC/DC" setAlbum:#"Back In Black" setPlayt:#"4:16"];
[mySong2 setTitle:#"Medicate" setArtist:#"AFI" setAlbum:#"Crash Love" setPlayt:#"4:21"];
[mySong3 setTitle:#"Rucci" setArtist:#"Austin TV" setAlbum:#"La ultima noche" setPlayt:#"4:39"];
[myPlayList1 addSong:mySong1];
[myPlayList1 addSong:mySong2];
[myPlayList2 addSong:mySong3];
[myPlayList1 showPlayList];
[myPlayList2 showPlayList];
[myPlayList1 showPlayList];
[myPlayList1 showAllSongs];
}
return 0;
}
//
// PlayList.m
// MyItunes
//
// Created by Rodrigo López on 6/29/12.
// Copyright (c) 2012 ITQ. All rights reserved.
//
#import "PlayList.h"
#implementation PlayList
#synthesize playListarray,playListName;
-(id) initWithName: (NSString *) name
{
if(self)
{
playListName = [NSString stringWithString: name];
playListarray = [NSMutableArray array];
playlistMaster=[NSMutableArray array ];
}
return self;
}
-(void) addSong: (Songs *) theSong
{
[playListarray addObject:theSong];
[playlistMaster addObject:theSong];
}
-(void) showPlayList
{
NSLog(#"Play List: %#", self.playListName);
for(Songs *theSong in playListarray)
{
NSLog(#"%#", theSong.title);
}
}
-(void) showAllSongs
{
NSLog(#"Play List: Master");
for(Songs *theSong in playlistMaster)
{
NSLog(#"%#", theSong.title);
}
}
-(NSUInteger) entries
{
return [playListarray count];
}
-(void) remove: (Songs *) theSong
{
[playListarray removeObjectIdenticalTo:theSong];
}
#end
The problem is with the NSMutable array, i want to declare the array globally, but i dont know how to do it, here is the output of this program:
2012-06-29 19:24:06.061 MyItunes[17184:707] Play List: First
2012-06-29 19:24:06.080 MyItunes[17184:707] Back In Black
2012-06-29 19:24:06.083 MyItunes[17184:707] Medicate
2012-06-29 19:24:06.084 MyItunes[17184:707] Play List: Second
2012-06-29 19:24:06.085 MyItunes[17184:707] Rucci
2012-06-29 19:24:06.089 MyItunes[17184:707] Play List: First
2012-06-29 19:24:06.090 MyItunes[17184:707] Back In Black
2012-06-29 19:24:06.091 MyItunes[17184:707] Medicate
2012-06-29 19:24:06.091 MyItunes[17184:707] Play List: Master
2012-06-29 19:24:06.092 MyItunes[17184:707] Back In Black
2012-06-29 19:24:06.093 MyItunes[17184:707] Medicate
So in play list master, its missing Rucci song, hope you can help me, thank you very much, i appreciate it
I would make your playlistMaster a "static variable" of the PlayList implementation file as described at: Objective C Static Class Level variables. You just declare and initialize it the .m file outside of any other methods. (Don't forget to make sure it is retained.)
I should also note that there are some memory issues in your PlayList class. In particular, your initializer code should retain the member variables, like:
-(id) initWithName: (NSString *) name
{
if (self = [super init]) {
playListName = [[NSString stringWithString: name] retain]; // NOTE: You could also do: [[NSString alloc] initWithString: name];
playListarray = [[NSMutableArray array] retain]; // NOTE: You could also do: [[NSMutableArray alloc] init];
playlistMaster = [[NSMutableArray array] retain]; // ditto
}
return self;
}
If you do this, you will need to add a dealloc method that releases each of these. But if you don't they may be released before you try to use them.
Related
I have a custom class that conforms to NSSecureCoding. I'm trying to encode its data, write it to a file, and decode it. The class contains an int array which I convert to an NSArray filled with NSNumber elements before encoding. When I decode the array, decodeObjectForKey returns nil. Also, when I write the encoded array to file, the plist doesn't look right. For example, it doesn't have the "key_for_ns_array" key anywhere in its structure.
Here's a simplified version of my code:
#interface c_my_data_wrapper : NSObject <NSSecureCoding>
#property (assign, nonatomic) struct s_my_data my_data; // the array exists as an int[] in here
#end
#implementation c_my_data_wrapper
- (void)encodeWithCoder:(NSCoder *)encoder
{
NSMutableArray *ns_array= [NSMutableArray array];
int_array_to_ns_array(self.my_data.my_int_array, count, ns_array);
[encoder encodeObject:ns_array forKey:#"key_for_ns_array"];
}
- (instancetype)initWithCoder:(NSCoder *)decoder
{
self= [self init];
if (self)
{
// this returns nil
NSArray *ns_array= [decoder decodeObjectForKey:#"key_for_ns_array"];
}
return self;
}
#end
c_secure_storage_data_wrapper* g_my_data_wrapper= nil; // allocation not shown
void store_data(struct s_storage_data_secure *new_data)
{
g_my_data_wrapper.my_data= *new_data;
NSData *encoded_data=
[NSKeyedArchiver archivedDataWithRootObject:g_my_data_wrapper
requiringSecureCoding:YES
error:nil];
// then I write the encoded data to a file
}
void load_data(void)
{
NSData *decoded_data= [NSData dataWithContentsOfURL:my_url];
g_my_data_wrapper=
[NSKeyedUnarchiver unarchivedObjectOfClass:[c_my_data_wrapper class]
fromData:decoded_data
error:nil];
}
void int_array_to_ns_array(
int *int_array,
int count,
NSMutableArray *out_ns_array)
{
for (int index= 0; index < count; ++index)
{
[out_ns_array addObject:[NSNumber numberWithInt:int_array[index]]];
}
}
If you're curious about the whole int array to ns array conversion thing... it's because I prefer to write in c++. I'm not very experienced with objective-c, so I only write it when I need to bridge the gap between my game and iOS. In this case, s_my_data is a type that's shared between the two.
One problem, probably the problem, is that you claim your class is NSSecureCodeing-compliant, but you're still calling -decodeObjectForKey: instead of -decodeObjectOfClass:forKey::
NSArray *ns_array= [decoder decodeObjectForKey:#"key_for_ns_array"];
That's not secure. The whole point of NSSecureCoding is that you tell the decoder what type you're expecting to get back, which is what -decodeObjectOfClass:forKey: does.
I am learning Objective C and I have tried to override my superclass (NSObject) description method in my BNRItem class. Though it seems like I have done everything right, my NSLog does not seem to use my overridden description method. Instead I see a Thread 1 Breakpoint 1.1 in my definition of the description method and more precisely, where I defined the descriptionString.
Here is my console output.
2015-08-30 20:49:00.622 RandomItems[46034:1002101] Zero
2015-08-30 20:49:00.623 RandomItems[46034:1002101] One
2015-08-30 20:49:00.623 RandomItems[46034:1002101] Two
2015-08-30 20:49:00.623 RandomItems[46034:1002101] Three
(lldb)
My main.m file:
#import <Foundation/Foundation.h>
#import "BNRItem.h"
int main(int argc, const char * argv[]) {
#autoreleasepool {
// Create a mutable array object, store its address in items variable...
NSMutableArray *items = [[NSMutableArray alloc] init];
//Send the message addObject: to the NSMutableArray pointed
//by the variable item, passing a string each time
[items addObject:#"One"];
[items addObject: #"Two"];
[items addObject: #"Three"];
// Send another message, insertObject:atIndex;, to that same array object
[items insertObject:#"Zero" atIndex:0];
// For every item in the items array ...
for (NSString *item in items){
//Log the description of item
NSLog(#"%#", item);
}
// Create a BNRItem instance and log its instance variables in the console
BNRItem *item = [[BNRItem alloc] init];
// Set item name to Red Sofa
item.itemName = #"Red Sofa";
item.serialNumber= #"A1B2C";
item.valueInDollards = 100;
//
NSLog(#"%#", item);
//Destroy the mutable array object
items = nil ;
}
return 0;
}
My header file:
#import <Foundation/Foundation.h>
#interface BNRItem : NSObject
{
NSString *_itemName;
NSString *_serialNumber;
int _valueInDollards;
NSDate *_dateCreated;
}
- (void)setItemName:(NSString *)str;
- (NSString *)itemName;
- (void)setSerialNumber:(NSString *)str;
- (NSString *)serialNumber;
- (void)setValueInDollards:(int)v;
- (int)valueInDollards;
- (NSDate *)dateCreated;
#end
And finally, my implementation file:
#import "BNRItem.h"
#implementation BNRItem
- (void)setItemName:(NSString *)str
{
_itemName = str;
}
- (NSString *)itemName
{
return _itemName;
}
- (void)setSerialNumber:(NSString *)str
{
_serialNumber = str;
}
- (NSString *)serialNumber
{
return _serialNumber;
}
- ( void )setValueInDollards:(int)v
{
_valueInDollards = v;
}
- ( int )valueInDollards
{
return _valueInDollards;
}
-( NSDate * )dateCreated
{
return _dateCreated;
}
- ( NSString * )description
{
NSString *descriptionString = [[NSString alloc] initWithFormat:#"%# (%#): Worth %d, recorded on %#", self.itemName, self.serialNumber, self.valueInDollards, self.dateCreated];
return descriptionString;
}
#end
It sounds like you have simply set a breakpoint on a line in your -description method. The debugger is stopping your program at the breakpoint. There's no indication of an actual error.
If you hit the continue button in Xcode, your program would probably proceed just fine.
You can disable the breakpoint, tell Xcode to ignore all breakpoints, or delete the breakpoint if you don't want to break there. The breakpoint will look like a blue arrow in the margins to the left of your code. Right-click or Control-click on it to see options.
To make Xcode ignore breakpoints, toggle the breakpoint button in the debugging toolbar. It also looks like a right-pointing arrow. It will be filled in blue if breakpoints are enabled. It will be an outline if they're disabled.
I am able to add NSImages to my NSCollectionView without having to first save them on disk. The images are fed into the collection view from an NSMutableArray. This way people can see the images without first having to save them.
Is there something similar that I can achieve with IKImageBrowserView? NSCollectionView is functional when it comes to representing images, but I would like to see if I can do something similar with IKImageBrowserView.
I can easily implement IKImageBrowserView with images saved on disk (Apple docs cover how this works) but can't figure out exactly where to look or how to go about adding images to the browser view directly from NSMutableArray instead of first saving them images to disk.
I'm at a loss here. Apart from the docs, I'm not really sure where else to look for direction. Or what to even call what I'm looking to do.
EDIT: (Here's some of the code)
// The data object -- if it is possible to represent an image object, this is where I am probably going wrong.
#interface ImageObject : NSObject
#property (readwrite, copy) NSImage *image;
#property (readwrite, copy) NSString *imageID;
- (id)initWithImage:(NSImage *)anImage;
- (NSString *)imageUID;
- (NSString *)imageRepresentationType;
- (id)imageRepresentation;
#end
#implementation ImageObject
#synthesize image = _image;
#synthesize imageID = _imageID;
- (id)initWithImage:(NSImage *)anImage
{
if (self = [super init]) {
_image = [anImage copy];
}
return self;
}
- (NSString *)imageUID
{
return _imageID;
}
- (NSString *)imageRepresentationType
{
return IKImageBrowserNSImageRepresentationType;
}
- (id)imageRepresentation
{
return _image;
}
#end
// This is how objects are supposed to be added to the browserView. All of this is straight from Apple.
- (void)updateDatasource
{
[_browserImages addObjectsFromArray:_importedImages];
[_importedImages removeAllObjects];
[imageBrowser reloadData];
}
- (NSUInteger)numberOfItemsInImageBrowser:(IKImageBrowserView *)aBrowser
{
return [_browserImages count];
}
- (id)imageBrowser:(IKImageBrowserView *)aBrowser itemAtIndex:(NSUInteger)index
{
return [_browserImages objectAtIndex:index];
}
This is where I try to add NSImages to the browserView but nothing happens. The array gets populated (which means the images are generated without any errors) but nothing happens on the screen.
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[oPanel URL] options:nil];
NSMutableArray *timesArray = [self generateTimeForSpecifiedNumberOfFramesInVideo:10 UsingAsset:asset];
self.imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
[[self imageGenerator] generateCGImagesAsynchronouslyForTimes:timesArray completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {
NSImage *testImage = [[NSImage alloc] initWithCGImage:image size:NSZeroSize];
if (result == AVAssetImageGeneratorSucceeded) {
ImageObject *objects = [[ImageObject alloc] initWithImage:testImage];
[_importedImages addObject:objects];
}
}
As for exploring the rest of the search results...been there done that. If I did miss anything, kindly mark this question as duplicate indicating what post already existed where this issue has been addressed.
EDIT:
I have accepted the answer below. Along with the unique IDs problem. I had overlooked a simple thing which was the requirement to call the updateDatasource method.
The most important point of using IKImageBrowser is create a unique image ID for each element. The following is an example. In fact, it comes from the project that I'm currently working on. I have just implemented IKImageBrowser in it. The code below assumes that you have 36 images (Image01.png, Image02.png..., Image36.png) imported into the project.
// .h
#interface AppDelegate : NSObject {
IBOutlet IKImageBrowserView *browserView;
NSMutableArray *imageArray;
}
// .m
#import "IKBBrowserItem.h"
#import <QuartzCore/QuartzCore.h>
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
imageArray = [[NSMutableArray alloc]init];
}
- (void)populateImage {
for (NSInteger i2 = 1; i2 <= 36 ; i2++) {
NSString *name;
if (i2 < 10) {
name = [NSString stringWithFormat:#"Image0%ld",(long)i2];
} else {
name = [NSString stringWithFormat:#"Image%ld",(long)i2];
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
NSImage *Image0 = [NSImage imageNamed:name];
NSInteger ran = [self genRandom:1000000:9999999];
NSString *imageID = [NSString stringWithFormat:#"%#%li",name,ran];
IKBBrowserItem *item = [[IKBBrowserItem alloc] initWithImage:Image0 imageID:imageID:name];
[imageArray addObject:item];
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
[browserView reloadData];
}
- (NSInteger)genRandom: (NSInteger)min :(NSInteger)max {
int num1;
do {
num1 = arc4random() % max;
} while (num1 < min);
return num1;
}
You don't need to use a random integer generator (genRandom) above, but just make sure that no imageID is the same.
This web site has a sample project, which should get you going. (I have no affiliation.) So make sure you download and run it. Then take a closer look and improve it for your needs.
I would like to release a Car object present in the dealer. I would like to know what is the right way of doing it. NSMutableArray Inventory stores the cars for a particular dealer. So, now I would like to present the user with a delete functionality, so, user could select the car using the Vin Number and delete it. But if I try to find the car and release the reference that doesn't works. Would I need to remove the object from the Array and then, release the reference? I am fairly new to objective c and in the initial learning phase. Thank you.
#import "Dealer.h"
#import "Address.h"
#import "PhoneNumber.h"
#implementation Dealer
static NSInteger dealerIdAllocater = 0;
-(id) init{
self = [super init];
self.dealerId = ++dealerIdAllocater;
self.addressList = [[NSMutableArray alloc] init];
self.inventory = [[NSMutableArray alloc] init];
return self;
}
#synthesize dealerId, name, addressList, inventory;
-(void)addCarInInventory:(Car*)car{
[self.inventory addObject: car];
}
-(void)displayAddresses{
for(Address *address in self.addressList){
NSLog(#"Street Address: %#", address.streetAddress);
NSLog(#"City: %#", address.city);
NSLog(#"State: %#", address.state);
NSLog(#"Country: %#", address.country);
for(int i=0; i<[address.phoneNumber count]; i++){
PhoneNumber *phoneNumber = [address.phoneNumber objectAtIndex:i];
NSLog(#"Phone Number %i, %#", i, phoneNumber.phoneNumber);
}
NSLog(#"--------------");
}
}
-(void)displayInventory{
for(Car *car in self.inventory){
[car displayInformation];
}
NSLog(#"--------------");
}
-(Car *)findCarById:(NSString *)vinNumber{
for(Car *car in self.inventory){
if ([vinNumber isEqualToString:car.vinNumber]) {
return car;
}
}
return nil;
}
#end
Would I need to remove the object from the Array and then, release the reference?
Yes, containers such as NSMutableArrays increment the retain count of objects by 1 when added to them. This is to make sure the container will always hold a valid reference to an object. When you remove an object from the container, the retain count will be decremented by 1.
I have a project written for iOS 4.x. Recently i update it to iOS5 using XCode 4.3.2. It's strange that the app stop everytime with double free error when using Apple LLVM compiler. After i change back to LLVM GCC, it works fine. Is there any difference between this two?
The code is shown below:
- (NSArray *)readCourselist {
NSString *path = [[self currentUserPathString] stringByAppendingPathComponent:kUserCourseListFilename];
return [NSArray arrayWithContentsOfFile:path];
}
- (NSArray *)getCourselist {
NSArray *courseRawArray = [self readCourselist];
for (NSDictionary *courseDic in courseRawArray) {
CourseModel *courseModel = [[CourseModel alloc] init];
courseModel.courseID = [[courseDic objectForKey:kKeyID] intValue];
courseModel.courseNameString = [courseDic objectForKey:kKeyTitle];
NSArray *lectureArray = [courseDic objectForKey:kKeyLecture];
for (NSDictionary *lectureDic in lectureArray) {
LectureModel *lectureModel = [[LectureModel alloc] init];
NSString *startString = [lectureDic objectForKey:kKeyStart];
if ([startString isEqualToString:#"8:00"]) {
lectureModel.lectureNumber = 1;
}
else if ([startString isEqualToString:#"9:50"]) {
lectureModel.lectureNumber = 2;
}
lectureModel.location = [lectureDic objectForKey:kKeyWhere]; //#property of location is retain
[courseModel.lectureArray addObject:lectureModel];
[lectureModel release];
}
[courseArray addObject:courseModel];
[courseModel release];
}
}
With more tracing i found out that it's
lectureModel.location = [lectureDic objectForKey:kKeyWhere];
that really matters.
In my LectureModel, location is declared as follow
#property (nonatomic, retain) NSString *location;
#synthesize location;
- (id)init {
location = NSLocalizedString(#"未知", nil);
}
Delete NSLocalizedString and everything works all right.
Why?
Generally working with NSDictionary you want to use valueForKey: instead of objectForKey:, but I don't think that's the problem here. If you flip it back to LLVM, and run Instruments with "Zombies", it should point you to exactly where each free (well, release) is occurring.