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...
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I follow the online way to write singleton, each into SecondViewController, address of the print is the same , but why UISwitch interface is initialized ? I was into the original UIViewController? someone was also suggested to write helper class. Everthing is beginning, it is not very clear, Is there any Demo or explain the basic principles?How to write the code depend on second image?
enter image description here
enter image description here
Try this code
static SecondViewController *sharedVCInstance = nil;
+ (SecondViewController *)sharedInstance
{
static dispatch_once_t onceToken = 0;
dispatch_once (&onceToken, ^{
if(sharedVCInstance == nil)
sharedVCInstance = [[SecondViewController alloc] init];
});
return sharedVCInstance;
}
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 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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to do something very simple, I'm trying to change a label to "I like to say hi" by clicking a button. I already know how to do it without NSString but I wanted to make it more harder, but I can't find what's wrong with this code. It's not working.
- (IBAction)button:(id)sender{
NSString *hi = #"hi";
_label.text = NSLog(#"I like to say %#", hi);
}
I'm also trying to do something even more complicated and use a void statement like this. Would this work, and if not what's wrong with it?
- (void)num{
int num = 42;
}
- (IBAction)button:(id)sender{
_label.text = NSLog(#"I like to say %i", num);
}
Why are you trying to use NSLog for this? That's for showing messages in the log.
You want NSString stringWithFormat:.
_label.text = [NSString stringWithFormat:#"I like to say %#", hi);
What you have won't even compile since the NSLog function does not return an NSString value.
NSLog is a function with a void return type, which means it does not return any value, so you can't use it to assign the value of a property. It is used only for printing messages to the debugging console or device logs. What you are probably looking for is stringWithFormat:
_label.text = [NSString stringWithFormat:#"I like to say %#", hi];
This is a method of the NSString class. It constructs strings similar to NSLog, but it actually returns a new NSString value that can be assigned to properties and so on.
Read its documentation here. You will be using this and other NSString methods all the time.
Regarding your other code:
If you want to construct a method named num that produces a value you can use elsewhere, you need to declare the type of the value it returns, instead of using void:
- (int)num{
return 42;
}
Now num is a proper instance method. You will need to tweak your code to be able to access this method correctly:
- (IBAction)button:(id)sender{
_label.text = [NSString stringWithFormat:#"I like to say %i", [self num]];
}
Here you are sending the message num to self. The keyword self is, loosely speaking, a reference to the current object instance. So [self num] will invoke the num method you have defined, and return its value. You may want to read Programming with Objective-C to learn more about these concepts.