I am trying to query the users in my Parse DB into my Table View Controller in my app in Xcode.
I am a beginner, but I have posted my code snippet from my added .swift file to my project. I am trying to query the user in the .swift file but it is not recognizing 'PFUser' from my View Controller.
Do I need to declare the PFUser as something in my .swift file before first calling it in my ViewDidLoad method?
Any ideas?
Code snippet:
You need to import the Parse-Framework in your project. Check this quickstart-guide how to do it.
If you've done it already, you need to import Parse in your File like that:
import UIKit
import Parse
Like you/Xcode has already done that with the UIKit.
First put Parse SDK into the folder into your project navigator area of Xcode.
You will get a Dialogue, You will need to check the box that says copy items if needed and then click Finish.
The next thing we need to do to connect our Swift app to parse.com is create a bridging header. To do this you need to create a new File (File -> New -> File) of type Objective-C File. Call this whatever you like. Here I have just called it testDataCustom. You will see another Dialogue that looks like having this Line at the top "Would you like to configure Objective C bridging Header?" Click on Yes.
Now it will add some new files to your project. Click on the File: testData-Bridging-Header.h. You should see this:
// Use this file to import your target's public headers that you would like to expose to Swift.
5.Underneath this comment code we need to add an #import so that our project knows to use the Parse iOS SDK. To do this we simply add a line into this file, like so:
#import <Parse/Parse.h>
Now you can successfully use Parse methods and properties.
Referred from this Link
You have to import the proper Parse file. :)
I don't know Swift but I assume that of you type
Import Parse.h
It will autocomplete for you.
From what I know in objective-c, you might need to import
Import <Parse\Parse.h>
Related
There are lots of question about how to use swift code in objective c. I had swift based project. there is Objectice C file where i need to use swift code.
Xcode did not create ProjectName-Swift.h automatically. So i created manually and check the following things.
Generated interface header name : ProjectName-Swift.h
Product Module Name : myproject
Defines Module : YES
Embedded Content Contains Swift : YES
Install Objective-C Compatibility Header : YES
Add #objc in swift class
import UIKit
import SwiftyJSON
#objc class User: NSObject, NSCoding
{
}
Then Import ProjectName-Swift.h in objective c file. But gives error Unknown Type name User
I had tried with add #class User It gives error forward reference using #class
How can I fix this erros
Hopefully you have already solved the problem after reading the documentation referenced by #AnupamMishra, but just in case: try removing from the project the ProjectName-Swift.h file that you created manually. It hides the file of the same name auto-generated by Xcode and not listed in your project. The file is still there, somewhere deep in the DerivedData directory.
Another observation: you didn't have to set Objective-c Generated Interface Header Name in Build Settings. Xcode would generate one and name it myproject-Swift.h by default, since myproject is your Product Module Name.
I have a subclass of NSOperation and custom init method which can take couple of arguments. this class is written in OBJ C. and I wanted to call this init method from Swift class for unit testing. is there a way I can call the init method directly from swift? Compiler couldn't recognize the init method.
You have to create a bridging header and import the ObjC class there. Then that class will be available in all of your swift classes
Add a new file to Xcode (File > New > File), then select “Source” and click “Header File“.
Name your file “YourProjectName-Bridging-Header.h”. ...
Create the file.
Navigate to your project build settings and find the “Swift Compiler – Code Generation” section. You may find it faster to type in “Swift Compiler” into the search box to narrow down the results. Note: If you don’t have a “Swift Compiler – Code Generation” section, this means you probably don’t have any Swift classes added to your project yet. Add a Swift file, then try again.
Next to “Objective-C Bridging Header” you will need to add the name/path of your header file. If your file resides in your project’s root folder simply put the name of the header file there. Examples: “ProjectName/ProjectName-Bridging-Header.h” or simply “ProjectName-Bridging-Header.h”.
Open up your newly created bridging header and import your Objective-C classes using #import statements. Any class listed in this file will be able to be accessed from your swift classes.
Above info was found here: http://www.learnswiftonline.com/getting-started/adding-swift-bridging-header/
I'm facing the following problem :
I've declared an protocol in a Swift file and I want to implement it in Objective-C.
I added the #objc annotation to my protocol so Xcode can generate a definition inside my (Project)-Swift.h file and it works fine.
However I cannot import the (Project)-Swift.h file in any of my objc headers since (I suppose) it is generated after those headers has been processed. It is not a big deal because I can simply use this workaround in my objective header file :
#protocol MySwiftProtocolName;
It compiles and works just fine but it will also throws a warning for each class that implements my swift protocol :
Cannot find protocol definition for MySwiftProtocolName
So my question is : Is it possible to force Xcode to generate the (Project)-Swift.h file before it starts processing the regular .h files ?
Thanks
You should be able to add #import "(Project)-Swift.h" into the file where you do implementing this swift protocol. Some times Xcode syntax gets funny and not autocomplete this header name, but in compile time it should work.
If it's not could you please provide more context of what is protocol looks like, as it may be not commutable with Objective-C and so it's not get generated at all.
You can't write to self.state in the subclass unless you import UIGestureRecognizerSubclass.h as indicated here.
In a Swift environment, I'm confused how I'd go about importing this. I tried import UIGestureRecognizerSubclass.h, and without the .h, but I still can't write to self.state.
How would I accomplish this?
The Swift equivalent is simply:
import UIKit.UIGestureRecognizerSubclass
That imports the appropriate header.
You need to have or create a -Bridging-Header.h file to import objc headers such as the one you want. The import line looks like this:
#import <UIKit/UIGestureRecognizerSubclass.h>
If you don't already have a bridge header file in your app, the easiest way to get one is to add an objc class to your project, and xcode will ask if you want one, then creates the file and ties it into the settings for you. You can then delete the objc class.
Everything in that header file is automatically made available to your Swift code, no need to add any import lines in your swift files.
i used following code showing error that file not found.
#import <SwiftDemo/ViewController-swift.h>
even ViewController.swift file is present in project, help me to sort out problem.
I assume you are trying to use Swift code from Objective-C. Is your product name "ViewController"? If not, then you are not importing the correct header. You should use the Xcode generated header ProductModuleName-Swift.h (replacing ProductModuleName with your specific product name).
In simplest terms, the ProductModuleName is just the name of your application. Example: my app is Application so this header is Application-Swift.h
Also, this header will connect ALL of your Swift to Obj-C, it is not file specific.
Please refer to this immensely helpful documentation piece on Swift/Obj-C interoperability.
You should import a synthetic file generated by the compiler called "<YourProductName>-Swift.h".
I think that this file is generated on the fly, but you can still inspect what it expands to by e.g. creating a dummy Objective-C file that contains just this:
extern int ____BEGIN_SWIFT_IMPORTS____; // dummy marker declaration
#import "<YourProductName>-Swift.h"
(you should of course substitute <YourProductName>)
and then by running the preprocessor on it (Product → Perform Action → Preprocess). All the output after the dummy marker declaration will come from the generated file. It's useful to see how Swift code marked with #objc gets translated to Objective-C declarations.