Can someone help me convert this OBJ-C code to Swift? - objective-c

Im trying to add the facebook audience network to my project but the tutorial is in objective C and I need it in swift. I already made the bridging header, now I need to add the code. Can someone convert it to swift for me? Thank you! Heres the link if you want to take a look at it:
https://developers.facebook.com/docs/audience-network/ios/banner
- (void)viewDidLoad
{
[super viewDidLoad];
FBAdView *adView = [[FBAdView alloc] initWithPlacementID:PLACEMENT_ID adSize:kFBAdSizeHeight50Banner rootViewController:self];
[adView loadAd];
[self.view addSubview:adView];
}

This should work, let me know if any trouble
override func viewDidLoad() {
super.viewDidLoad()
let adView: FBAdView = FBAdView(placementID:PLACEMENT_ID, adSize:kFBAdSizeHeight50Banner, rootViewController:self);
adView.loadAd();
self.view.addSubview(adView);
}

Related

'initWithNibName:bundle:' is unavailable LBTAFormController

I am new to swift, and i am working on a swift and objective-c mixed project. I got stuck , if anyone could help, Thanks so much!
I would like to use LBTAFormController(LBTATools).
In my project, I created FormViewController.swift and FormViewController.xib.
I want to pushViewController to FormViewController.swift
FormViewController * loginForm = [[FormViewController alloc] initWithNibName:#"LoginFormViewController" bundle:nil];
[self.navigationController pushViewController:loginForm animated:YES];
in FormViewController.swift, I set up like this:
import UIKit
import LBTATools
class FormViewController: LBTAFormController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
However, I always got error
'initWithNibName:bundle:' is unavailable

UISwipegesturerecognizer to change ImageView positions on the view controller?

I'm an Objective-C beginner and I've found similar answers on stackoverflow to my question, but even after trying all of the tips/advice I'm still lost.
I have an IBOutlet UIImageView, and I want it to be so that if the user swipes up or down on this Image, then the position of various images on the view controller change.
I'm very green with the UISwipegesturerecognizer coding, so please explain in a way a beginner would understand. Thank you very much in advance!!!
UPDATE:
#Axeva, I used the following code, and there are no caution warnings or errors. However, it isn't executing properly on my simulator. Here is my code :
interface math0 (){
UISwipeGestureRecognizer *swipeUp;}
- (void)viewDidLoad
{
[super viewDidLoad];
swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: #selector(swipedScreenUp:)];
[swipeUp setNumberOfTouchesRequired: 1];
[swipeUp setDirection: UISwipeGestureRecognizerDirectionUp];
[one addGestureRecognizer: swipeUp];
[swipeUp setEnabled: NO];
}
- (void)swipedScreenUp:(UISwipeGestureRecognizer*)swipeGesture {
// Sampling to see if it works
instructions.text = [NSString stringWithFormat:#"IT WORKED!"];
}
In my .h file, I declared - (void)swipedScreenUp:(UISwipeGestureRecognizer)swipeUp;. On my storyboard, I set my image (named one) so that it was accessibility and user interaction enabled.
I've also tried with [swipeUp setEnabled: YES]; in my viewDidLoad file.
Can you or anyone tell me where the error is? Thank you!!
Try something like this:
#interface YourViewController () {
UISwipeGestureRecognizer *swipeLeftToRightGesture;
}
- (void)viewDidLoad {
[super viewDidLoad];
swipeLeftToRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: #selector(swipedScreenRight:)];
[swipeLeftToRightGesture setNumberOfTouchesRequired: 1];
[swipeLeftToRightGesture setDirection: UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer: swipeLeftToRightGesture];
}
- (void)swipedScreenRight:(UISwipeGestureRecognizer*)swipeGesture {
// Move your image views as desired
}
You can adjust this basic code to do exactly what you wish. (different swipe directions; etc.)

'Incompatible pointer types assigning to 'UIImage *' from 'UIImageView *'? Help please

So I'm creating a camera app for my Multimedia Major and I've come across this error in my 'VSViewController.m' file;
self.image.image = _image;
"'Incompatible pointer types assigning to 'UIImage *' from 'UIImageView *'"
Any help is appreciated!
(BELOW IS THE BLOCK OF CODE IT WAS IN)
#import "VSViewController.h"
#interface VSViewController ()
#end
#implementation VSViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Image Picker Delegate
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.image.image = _image.image;
[picker dismissViewControllerAnimated:YES completion:nil];
}
Consider the following code.
func test() {
var image = UIImage(named: "default.png") // UIImage
var imageView = UIImageView() // UIImageView
image = imageView.image // assigns image (UIImage) to imageView.image (UIImage)
}
You must assign a UIImage to your image, not a UIImageView. Use the UIImageView's image property to get compatible pointer types.
Alright, so now that we know what you're doing it looks like you should be extracting the image out of the imagePicker's dictionary like so:
self.image.image = [info objectForKey:UIImagePickerControllerOriginalImage];
That assumes that self.image is a UIImageView, but again I would recommend coming up with better names.
I came across two ways of solving this error after I got the same error from my first blind attempt.
One was to use the image property of my UIImageView's ivar/property, as Brian above mentioned. But when I did that, I found I had to constantly call this image property along with its UIImageView property/variable, as in I was calling: self.UIImageViewiVar.image rather than just self.UIImageViewiVar.
Another way to do it is via the initWithImage: method like so:
self.UIImageViewiVar = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"XYZ.jpeg"]];
And thereafter you can just call on self.UIImageViewiVar.
Hope this helps someone.

How to use NSWindowController to show a window in standard application?

I created a new blank standard application using Xcode template. Removed the window in MainMenu.xib and I created a new customized NSWindowController subclass with a xib.
They were named "WYSunFlowerWindowController.h" and "WYSunFlowerWindowController.m".
And I append then init function like below:
- (id)init
{
NSLog(#"init()");
return [super initWithWindowNibName:#"WYSunFlowerWindowController" owner:self];
}
And my WYAppDelegate.m file is like below:
static WYSunFlowerMainWindowController* windowController = nil;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
if (windowController == nil) {
windowController = [[WYSunFlowerMainWindowController alloc] init];
}
[[windowController window] makeKeyAndOrderFront:windowController];
}
And I have the problem, that the window can't show it self after I launch the app. Anyone can tell me why? Is anything wrong with my code?
I am a newbie in Objective-C and cocoa. So I think I maybe make a silly mistake that I can't figure it out by myself.
UPDATE:
Here is my project source. Pleas have a look and help me to figure out what is my mistake。
https://dl.dropbox.com/u/3193707/SunFlower.zip
In your init method, I think you have to set self to the super init first before you return self.
-(id)init
{
NSLog (#"init()");
self = [super initWithWindowNibName:#"WYSunFlowerWindowController" owners:self];
return self;
}
Edit:
Try replace makeKeyAndOrderFront: with [windowController showWindow:self]
Then if that still doesn't work, check your window controller xib, make sure the file owner is set to WYSunFlowerWindowController and that the IBOutlet Window (declared in NSWindowController) is connected to the window.
Edit 2:
Commenting out your #property and #synthesize window in your controller was the trick. Don't redeclare get and setters that were already predefined in a superclass.

Segue code not working from Apress book Beginning iOS 5 Chapter 10 on Storyboards

First, let me say that I'm pretty new to Objective-C and iOS programming in general so forgive me if the answer for this is obvious to you guys.
So I'm now going through the Apress Beginning iOS 5 book and in Chapter 10 about Storyboards, I encounter the following problem when trying to Run one of the examples.
As I understand it, this block of code is supposed to pass the text of a task from a list to a text input field on the next page. Here's the code exactly from the book, but I get this error when trying to Build and Run the code: Property 'tableView' not found on object of type 'BIDTaskListController *'.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *destination = segue.destinationViewController;
if ([destination respondsToSelector:#selector(setDelegate:)]) {
[destination setValue:self forKey:#"delegate"];
}
if ([destination respondsToSelector:#selector(setSelection:)]) {
// prepare selection info
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; // Error on this line
id object = [self.tasks objectAtIndex:indexPath.row];
NSDictionary *selection = [NSDictionary dictionaryWithObjectsAndKeys:
indexPath, #"indexPath",
object, #"object",
nil];
[destination setValue:selection forKey:#"selection"];
}
}
Also, I'm running Xcode 4.4 by the way. Any help would be much appreciated. Thanks.
Regards,
Tim
You have to put this code inside UITableViewController, propable you're using UIViewController, so you don't have reference to tableview or you can add UITableView to your UIViewController and connect IBOutlet to it.