In Xcode, for Obj-C programs, is there a way to generate skeleton method body in the .m file if I add a new method or implement an interface in the corresponding .h file? Like in Eclipse, if you implement an interface, it will bring in the method skeleton according to the definition of the interface.
if you do a lot of OS X/iOS development, the best option i know of (for objc) is Accessorizer.app.
Related
Note: I know How to call Objective-C code from Swift, but I don't know below,
I want to use this EsptouchForIOS's Demo in my project. The demo is write in OC, it has a storyboard and controller. I want to know how to integrate the demo in my swift project, and use that storyboard and it's controller in my swift project.
I'll start writing from the very beginning. Suppose you have a project in Objective-C and now you want to continue your project's development in Swift. Follow the below guidelines: (This intends to your specific needs)
First choose to add a new file from File->New->File. In this process select your language as Swift. In the final step here, you will be prompted to Create Bridging Header. Select that:
Now build your project once (⌘+B). You may get an error like this:
Change your target's minimum deployment to the version that Swift supports. (Example in the below screenshot)
To use Objective-C resources in Swift files:
Now that you've got one ProjectName-Bridging-Header.h file in your project. If you want to use any Objective-C class in your Swift files, you just include the header file of that class in this bridging header file. Like in this project, you have ESP_NetUtil and ESPViewController class and their header files too. You want to expose them to Swift and use them later in Swift code. So import them in this bridging header file:
Build once again. Now you can go to your Swift file. And use the Objective-C classes as like you use any resource in swift. See:
N.B: You must expose all the class headers (that you're intending to use later in Swift) in that bridging header file
To use Swift resources in Objective-C files:
Now you may wonder, I've successfully used Objective-C resources in Swift. What about the opposite? Yes! You can do the opposite too. Find your Target->Build Settings->Swift Compiler - General->Objective-C Generated Interface Header Name. This is the header file you will be using inside your Objective-C classes for any Swift to Objective-C interoperability. To know more check here.
Now inside any of your Objective-C class, import that interface header and use Swift resources in Objective-C code:
You will get more understanding from the official apple documentation.
You can checkout the worked out version of your linked project here with Objective-C-Swift interoperability.
So according to your question, you have added an objective C bridge in your swift project using How to call Objective-C code from Swift.
Now, import all headers (.h) files of your objective-c source code (demo project) that you want to direct use in swift file.
For example, your demo project has EsptouchForIOS following header (file with extension .h) files in project source code.
ESPAppDelegate.h, ESPDataCode.h, ESPTouchDelegate.h
import a header file in your bridge, which you want to use in your swift code. Suppose in your swift code you want touch delegate ESPTouchDelegate then write,
#import "ESPTouchDelegate.h"
Here is snapshot of your demo integration in my Test Swift project with bridge
and import statements.
Now, there is function/method in an objective C file getValue
which is used/accessed in swift project/file.
Similarly, you can import as many files (source headers) as you want in bridge and use the same files (source code) in swift.
I have never tried to use objective-c from swift project. But I normally used swift classes from my objective-c project. I usually follow this instructions https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html from apple developer website.
I am confused when working with Xamarin obj-c bindings, Lets say if I am having a single .h file then its just an happy path when creating binding. But when a .h file is referenced with another .h in it as an import statement. How to deal with the bindings.
If I am having AB.h file which has A.h referred in it, what is the way to generate bindings for this. And If I am having a .xib file in obj-c project then how to deal with that.
Example Sake I am using this project for conversion.
https://github.com/hightower/HTHorizontalSelectionList/tree/master/Source
Have you looked at the Objective Sharpie, it will automatically identify the dependency and help you get started with creating iOS bindings
I have an Objective-c file which used to work with the older XCode's
with the emergence of XCode 6.x - code no longer performs
I found the same functionality in code written in Swift online
I read Apple's documentation about using bridging header's to allow the use of Swift with Objective-C. I was able to get that setup ok, I do believe.
What is not clear is - can I simply cut and paste the Swift code into my Objective-C file (a viewcontroller.me file actually), or am I limited to having to use a Swift file from the start ?
If I have to use a new file in my (very simple) app, Im not quite sure how to pull off having two viewcontroller.m files do the work where one used to work fine, so this method obviously creates a new problem for me.
Thanks for your assistance in advance !
I would recommend just creating a class or struct for your swift code, and access that class or struct in your Objective-C ViewController.
There is no main() method in swift. The program must start the execution from somewhere. So what is the entry point of swift code execution and how is it decided?
The entry point in a plain Swift module is the file in the module called main.swift. main.swift is the only file which is allowed to have expressions and statements at the top level (all other Swift files in the module can only contain declarations).
Cocoa Touch uses the #UIApplicationMain attribute on an implementation of UIApplicationDelegate instead of a main.swift file to mark the entry point. Cocoa used to use a minimal main.swift file which simply called NSApplicationMain, but as of Xcode 6.1 uses the #NSApplicationMain attribute on an implementation of NSApplicationDelegate.
In the AppDelegate.swift file you can see #UIApplicationMain.
The AppDelegate is the initial entry file.
Basically: main.m and AppDelegate.m are kinda merged in Swift to just AppDelegate.swift
You may want to read Files and Initialization
The exception is a special file named “main.swift”, which behaves much
like a playground file, but is built with your app’s source code. The
“main.swift” file can contain top-level code, and the order-dependent
rules apply as well. In effect, the first line of code to run in
“main.swift” is implicitly defined as the main entrypoint for the
program. This allows the minimal Swift program to be a single line —
as long as that line is in “main.swift”.
In Xcode, Mac templates default to including a “main.swift” file, but
for iOS apps the default for new iOS project templates is to add
#UIApplicationMain to a regular Swift file. This causes the compiler
to synthesize a main entry point for your iOS app, and eliminates the
need for a “main.swift” file.
Alternatively, you can link in an implementation of main written in
Objective-C, common when incrementally migrating projects from
Objective-C to Swift.
In Swift 5.3 there is a new #main attribute which lets you control where your entry point is in your project rather than just main.swift. There can only be one main entry and you can't have a main.swift file and a an attribute #main. See https://github.com/apple/swift-evolution/blob/master/proposals/0281-main-attribute.md for more details.
#main
struct App {
static func main() {
print("Starting.")
}
}
In Swift apps there are attributes:
#UIApplicationMain (Cocoa Touch)
#NSApplicationMain (Cocoa)
that tell the swift compiler where is the entry point of the application.
What swift compiler does under the hood is that it creates a main function, which basically looks the same as in Objective-C apps and treats this method as the app's entry point (a first method that is called when the application process is started).
If you want to read more about what swift compiler does with Main attributes, how the OS knows where is the entry point of the application, I encourage you to read this article: Understanding iOS app entry point
The entry point in a plain Swift module is the file in the module called.
Is it true that everything you do in Interface Builder can be done programmatically? If your project uses Interface Builder to make the GUI, can that code be converted to native Xcode and inserted into the project?
It is true that anything you can do in Interface Builder you can do programatically. However, IB does not generate code, so there is not really anything to 'convert' to source code for your project.
Interface Builder creates archived objects -- serialized instances of class instances -- and works directly from those. While you can reuse your .xib files (which some people still insist on calling "nibs") in any number of projects, you should note that:
Your nib is already code: it's just very verbose and hard-to-read XML. This XML contains the serialized (say "archived") instances.
You cannot "convert" your xib to Objective-C code, or at least Xcode does not provide a way to do this (and I don't know who would).
If you do not have the classes that the xib expects, you will run into errors using your xib.
I wrote a utility to convert .xib files to code. It's still experimental, but should do the majority of the grunt work converting to code:
https://github.com/Raizlabs/Eject
You can try it out online:
https://eject.herokuapp.com/