Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Here's my project: https://github.com/a8b/musicManager/tree/master/musicManager
I have issue with addSongToLibrary method in MusicCollection.m - it doesn't add songs with [library addSong:song]
To simplify your code for clarity, it's doing this:
-(void) addSongToLibrary:(Song *)song {
for (Song *song in library.songs) {
NSLog(#"%#", song);
}
[library addSong:song];
}
}
You are declaring a variable with the same name as a method parameter, so it is hiding the value passed in.
Well you commented out the following code
/*-(instancetype) init {
self = [super init];
if (self) {
library.songs = [NSMutableArray array];
[library addSong:[Song new]];
}
return self;
}*/
so library is never instaniated, either you uncommented it , or in viewDidLoad you need to add library.songs = [NSMutableArray array]; otherwise it will be nil.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am developping an application that saves informations about the logged-in user in CoreData and singleton class. After the user log-in, I'm fetching data from coredata and set the variables from the singleton.
My question:
If the app receives memory warning issue, and the data from the singleton will be released, my app will crash. What can I do in this situation?
Thanks!
Let's say you have a local property named NSArray *myArray in singleton's .m file where you store all the needed data. All you need to do is to add a method in header file which returns that array if is not nil, and in case of nil make it reload from storage and return.
Also override - (void)didReceiveMemoryWarning method and save the data in case of memory warning.
Here is a sample code written in objective-c:
//Singleton.h file
- (NSArray *)storedData;
//Singleton.m file
#property NSArray *myArray;
...
- (NSArray *)storedData
{
if (_myArray == nil)
_myArray = [self fetchMyArrayFromLocalStorage];
return _myArray;
}
- (NSArray *)fetchMyArrayFromLocalStorage
{
//Some code to fetch data from local storage
}
- (void)saveMyArrayToLocalStorage
{
//Code to save _myArray to local storage
}
- (void)didReceiveMemoryWarning
{
[self saveMyArrayToLocalStorage];
_myArray = nil; //Remove array if is needed
[super didReceiveMemoryWarning];
}
Now you'll always get the data you needed simply by calling method:
[[mySingleton sharedInstance] storedData]
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
Your ViewController would have this method by default, and before your app crash, this method will execute automatically, you should write some code in this method to make sure your data can be saved in device, and then release it.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I am using DZNPhotoPickerController which is written in Objective-C.
There are codes here that I can't convert to Swift.
//THIS CODES ARE ALREADY IN SWIFT AND ARE WORKING FINE
DZNPhotoPickerController *picker = [DZNPhotoPickerController new];
picker.supportedServices = DZNPhotoPickerControllerService500px | DZNPhotoPickerControllerServiceFlickr | DZNPhotoPickerControllerServiceGoogleImages;
picker.allowsEditing = NO;
picker.cropMode = DZNPhotoEditorViewControllerCropModeCircular;
picker.initialSearchTerm = #"California";
picker.enablePhotoDownload = YES;
picker.allowAutoCompletedSearch = YES;
//THIS CODES ARE ALREADY IN SWIFT AND ARE WORKING FINE
//HERE----------------------------
[picker setFinalizationBlock:^(DZNPhotoPickerController *picker, NSDictionary *info){
//some codes
}];
[picker setFailureBlock:^(DZNPhotoPickerController *picker, NSError *error){
//some codes
}];
picker.cancellationBlock = ^(DZNPhotoPickerController *picker){
//some codes
};
//HERE----------------------------
Can anyone transform the codes to Swift?
I'll help you with closures as I expect they are the most hard part of the code. Rest of the stuff is pretty similar to Java and other OOP languages.
You can check closure syntax here. Correct me if my syntax is wrong:
picker.finalizationBlock = {(picker, info) in
//Your code here...
}
picker.failureBlock = {(picker, error) in
//Your code here...
}
picker.cancellationBlock = {(picker) in
//Your code here...
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am quite new in iOS programming but I need it to test out a prototype.
I am receiving a value over bluetooth, which changes when a button is pressed on the physical prototype.
In my app a action has to follow when the button is pressed, therefore I need to know when the value has changed.
I have tried everything I could think of and looked everywhere but found no solution.
This is my current code:
-(void)bean:(PTDBean *)bean didUpdateScratchNumber:(NSNumber *)number withValue:(NSData *)data{
int value = *(int*)([data bytes]);
NSString *lastvalue = nil;
NSString *newValue = [NSString stringWithFormat:#"%d", value];
NSLog(#"ScratchWaarde: %d", value);
if ([newValue isEqualToString:lastvalue]) {
NSLog(#"Last Value: %#", lastvalue);
}else{
NSLog(#"Pushed");
lastvalue = newValue;
}
}
Thank you for helping!
Yeah, it looks like the OP expects lastValue to be kept as state on the object, so...
// in this class's interface
#property(strong,nonatomic) NSString *lastValue;
-(void)bean:(PTDBean *)bean didUpdateScratchNumber:(NSNumber *)number withValue:(NSData *)data{
// it looks like the OP is trying to get a string from the data, so...
NSString *newValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"ScratchWaarde: %d", value);
if ([newValue isEqualToString:self.lastvalue]) {
NSLog(#"Last Value: %#", self.lastvalue);
} else {
NSLog(#"Pushed");
self.lastvalue = newValue;
}
}
You code makes no sense. You are saying:
NSString *lastvalue = nil;
NSString *newValue = // ...
if ([newValue isEqualToString:lastvalue]) {
Under no circumstances will newValue ever be equal to lastValue, because lastValue is guaranteed to be nil, because that is the value you are setting it to in the first line (and newValue, because you are setting it to an actual NSString, is guaranteed not to be nil).
So, while I'm not clear on what exactly you are trying to accomplish, this obviously isn't it. It's not so much a programming matter as a matter of simple logical thought. The program can only do what you tell it to do, and what you are telling it to do is silly.
(In all probability what you are not grasping is that lastvalue is purely local to this method, so it is new every time your method is called. If you want a persistent lastvalue, you need a property global to the class, not a local variable.)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I need to display pdf documents within my iPad app, I've seen people suggesting loading it within a webview.
I want to know if this is the best\recommended way to display a pdf?
Using a QuickLook.framework you can easly load pdf and disply i just add following easy step for load pdf using QuickLook.framework
Add QuickLook.framework and import in to your class and set it's DataSource in to .h class <QLPreviewControllerDataSource>
Use following method:
-(NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
return 1;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
return [NSURL fileURLWithPath:self.pdfFilePath]; // here is self.pdfFilePath its a path of you pdf
}
and for load set Button Action:
-(IBAction)LoadPdf
{
QLPreviewController* preview = [[[QLPreviewController alloc] init] autorelease];
preview.dataSource = self;
[self presentModalViewController:preview animated:YES];
}
Two easiest way to that
UIWebView
QLPreviewController
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am trying to call BOOL in +(void) method but I can't. Why it would not set in +(void) method? While it is working on all -(void) methods.
.h
#property (nonatomic, assign) BOOL line;
.m
+ (void)normalizeCell:(UITableViewCell *)cell withLables:(NSArray *)lbls sx:(CGFloat)sx widths:(NSArray *)widths
if (![cell.contentView viewWithTag:22])
{
UIImageView *gb = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"table.png"]];
gb = 22;
[cell.contentView addSubview:gb];
[gb release];
}
You can't use your header property in class method (+), only in instance method (-).
This is some kind of static method known from other programming language. In objective-c you can use class method to do some operation which fit to class you've created, but you have to remember that using a class method is NOT an object operation. You don't have to creating objects to use class method and of course you can't access properties of the objects.
+ methods are class level methods and properties are instance level variables. Therefor it's not possible to set them, on what instance would they be set? Class level methods should not be used if you need to keep state. You can keep state if you really want to like this.
+ (BOOL)myBool:(NSNumber *)boolValue{
static BOOL myBool = false;
if (boolValue){
myBool = [boolValue boolValue];
}
return myBool;
}
If you want this to not be part of the public interface just put this directly in the .m file so it's invisible for other classes. Then when you are inside your other class method you can do.
BOOL b = [self myBool:nil]; // get value
[self myBool:[NSNumber numberWithBool:YES]]; // set value
If you for reason would like to access this from your instances you can like this.
BOOL b = [MyClass myBool:nil];
[MyClass myBool:[NSNumber numberWithBool:NO]];