Objective C to Swift

Hey Apple Community,


i am currently learning iOS Programming with Swift and a little bit Objective C and now i am at a point i don't move forward.
I do not understand how to make the following things work:
I have a Facebook Login written in C, and a Parse Login written in Swift and in the future I want to write in Swift.

So i need a few contact details (first name, last name, email, id, facebookurl..) from the Objective C converted to Swift, so i can use it their.

I read the apple documentation but i don't understand it.

So what i must change or do to use this data in Swift.
Here is my code from the facebook login:


Thanks a lot! 😉


#import "LoginViewController.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import "DashboardViewController.h"
#import "MBProgressHUD.h"
@interface LoginViewController (){
    /!
     *properties of name, email,fbid and fburl.
     */
    NSString* name;
    NSString* email;
    NSString* fbid;
    NSURL *facebookurl;
    }
@end
@implementation LoginViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    /
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    /
}
/!
*Fb signin button clicked.
*/
- (IBAction)facebookLoginClicked:(id)sender {
/
    [[NSUserDefaults standardUserDefaults]setObject:@"facebookSuccess" forKey:@"loginSuccess"];
    [[NSUserDefaults standardUserDefaults]synchronize];
/
        FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
        [login logInWithReadPermissions:@[@"email", @"user_friends", @"public_profile"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
          
             if (error) {
                 NSLog(@"Process error");
             } else if (result.isCancelled) {
                 NSLog(@"Cancelled");
             }
/
             else {
                 if ([result.grantedPermissions containsObject:@"email"])
                 {
                     NSLog(@"Logged in");
                     NSLog(@"result is:%@",result);
                     [self fetchedUser];
                 }
             }
         }];
}
/!
*This contains details of the fetched user details.
*/
-(void)fetchedUser{
    NSLog(@"the user token is :%@", [FBSDKAccessToken currentAccessToken]);
   
    /
   
    if ([FBSDKAccessToken currentAccessToken] != nil && ![[FBSDKAccessToken currentAccessToken]  isEqual: @"null"])
    {
        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {
/
                 name = result[@"name"];
                 email = result[@"email"];
                 fbid = result[@"id"];
                 facebookurl = [NSURL URLWithString:[NSString stringWithFormat:@"https:/
/Alter method to access the profile pic*/
/

                
/
                 [[NSUserDefaults standardUserDefaults]setObject:name forKey:@"name"];
                 [[NSUserDefaults standardUserDefaults]synchronize];
                 [[NSUserDefaults standardUserDefaults]setObject:email forKey:@"email"];
                 [[NSUserDefaults standardUserDefaults]synchronize];
                 [[NSUserDefaults standardUserDefaults]setObject:fbid forKey:@"fbid"];
                 [[NSUserDefaults standardUserDefaults]synchronize];
                 [[NSUserDefaults standardUserDefaults]setObject:[facebookurl absoluteString] forKey:@"facebookurl"];
                 [[NSUserDefaults standardUserDefaults]synchronize];
                
/
                 [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                 [self performSegueWithIdentifier:@"facebookSuccess" sender:self];
                 [MBProgressHUD hideHUDForView:self.view animated:YES];
             }
             else
             {
                 NSLog(@"Error %@",error);
             }
         }];
       
    }
}
@end

Unfortunately, it's very unlikely that anyone here will simply rewrite a slab of code for you. You either have to ask a more targeted question, or at least show what you've tried.


Are you saying you don't know Objective-C well enough to know how to write the same code in Swift?


If so, you can use a little knowledge plus some guesswork. An Obj-C method is called like this:


   [someObject someMessage…];


and the Swift equivalent is:


  someObject.someMessage (…)


Beyond that, Xcode autocomplete is your friend. Once you start typing 'someMessage', a template for the parameters should pop up, and you can press Return to insert it into your source. Then use the template to provide the actual parameters.


An Obj-C construct like this:


[[SomeClass alloc] initWithParameter: …];


corresponds to a Swift initializer like this:


SomeClass (parameter: …)


Other than that, leave out the "@" symbols preceding the string and array literals, and you should have most of the above code "decoded".

A good way is to look at 2 existing codes, in ObjC and in Swift. Tehre are examples in Apple's doc, such as the lister program ; that will show you most of translation cases.


Another bit of help:

NSString* name;

turns into

var name : String

Objective C to Swift
 
 
Q