Generate custom init method automatically - ios

Is it possible to generate custom init method automatically from Xcode, as Android Studio does for android?
I mean, if I declare some properties in .h, for example:
int a;
int b;
So, I would like to create automatically a init method as:
- (id)initWithA:(int) aInner andB:(int) bInner
{
a = aInner;
b = bInner;
}

New Xcode (after Xcode 10) support this issue for class (not for struct).
Steps:
right click on class name
click "refactor"
click "Generate Memberwise Initializer"
As for struct. You can make it class first, and change it back to struct after you get auto-gen init.

While there's still no way to do this automatically (without installing a plugin), there's a neat trick to convert a list of property declarations to assignments using multiple cursors:
Copy the list of properties and paste it into your constructor
Use Shift+Ctrl + click to insert multiple cursors (Shift+Ctrl+↑/↓ work as well)
Edit with multiple cursors to assign values
Credits go to Paul Hudson: Xcode in 20 Seconds: Multiple cursors

There is no native way of doing this, however you can install XCode extensions that will add support for this.
See this following extension as this will provide the feature you are after (Swift version).
https://github.com/Bouke/SwiftInitializerGenerator

Yo can create a snippet. You need to play with it a bit, create nice blue placeholders where relevant but most importantly, attach a keyboard shortcut to it. For example "initx"
Then you just start to type the shortcut in line where you want the initialiser to be and voila, you have your custom init.

Yes you can initialise an class by custom init method or you can pass parameter when you want to initialise class with custom init method
look
Define in your class.h file
#interface CustomView : UIView
- (id)initWithA:(int) aInner andB:(int) bInner
In .m file implement initWithStringMethod.
- (id)initWithA:(int) aInner andB:(int) bInner
{
if((self = [super init]))
return self;
}
initialised class from other class or viewController
CustomView *cv = [[CustomView alloc] initWithA:5 andB:10 ];

Related

Cannot use all methods in Objective-C class in Swift

I am trying to make use of an Objective-C API in Swift. I can only seem to call the shareMyInstance() method from Swift, and not the initOpenApi() method for some reason. I'm not sure if there is some sort of scope identifier present in the interface, but I can't make use of initOpenApi, even though both are in the header. I also cannot see the method bodies, but I don't believe that affects the scope of the function.
This is locking me into using Objective-C, because for some reason I can access all of the functions from Objective-C, but only 3 of the 4 from Swift.
Header file (LCOpenSDK_Api.h):
#ifndef LCOpenSDK_LCOpenSDK_Api_h
#define LCOpenSDK_LCOpenSDK_Api_h
#import <Foundation/Foundation.h>
#interface LCOpenSDK_Api: NSObject
+ (LCOpenSDK_Api*) shareMyInstance;
- (id) initOpenApi:(NSString*)addr port:(NSInteger)port CA_PATH:(NSString*)caPath;
- (NSInteger)request:(void*)req resp:(void*)resp timeout:(NSInteger)timeout;
- (void)uninitOpenApi;
#end
#endif
My code (.swift):
import Foundation
#objc(LeChangePlayerView)
class LeChangePlayerView: UIView {
//...
#objc
override init(frame: CGRect) {
super.init(frame: frame)
var lc = LCOpenSDK_Api.shareMyInstance()!; //Fine
//Need this function!
lc.initOpenApi("openapi.easy4ip.com", 443, "") //Value of type 'LCOpenSDK_Api' has no member 'initOpenApi'
}
The only possible other explanation is that there is a different header file with the same name, but different interface, but this is highly unlikely because shareMyInstance, request and unitOpenApi are all available, and going to the definition from within the swift file using Xcode points to the same file. It is a dynamic framework, and as of right now, I can only view the headers, not the method implementations. I'm not sure if there's a solution to this, but this is another problem I could use help with. Could they have locked the original source code somehow, as well as made that specific method private?
Although initOpenApi is an instance method, Swift recognises it as an initialiser as it starts with the word init. Initialisers are translated from Objective-C into Swift-style initialisers.
In Objective-C you would say something like [[LCOpenSDK_Api alloc] initOpenAPI:#"openapi.easy4ip.com", port: 443, CA_PATH: #""]
In Swift the word init is stripped and there is no need to explicitly allocate a new instance:
let lc = LC_OpenSDK_Api(openApi:"openapi.easy4ip.com:, port: 443, CA_PATH:"")
However, you need to refer to the documentation from the framework to determine if you want to access the singleton instance LC_OpenSDK_Api.shareMyInstance or whether you want to create a specific instance as I showed above.

Calling to a variable of swift view controller file in Objective-C

I have a project that is a cross of Swift and Objective-C using a bridging-header.
In my main ViewController.swift file, outside of the class declaration, I have this code:
var mainView = ViewController()
In other views that I segue to, I can use this to call a function to run back on the main ViewController by using mainView.runFunction()
How can I call this function in an Objective-C .m implementation file?
Thanks!
First of all for using swift in objective-c you need to import TargetName-Swift.h. Note that it's the target name.
For more information look at this.
You can achieve what you want in this way:
ViewController *mainView = [[UIViewController alloc] init];
[mainView runFunction];
Also you should declare your runFunction with #objc to use it in objective-c like below:
#objc func runFunction {
// what you want to do ...
}
Follow this apple article and done : Load Swift in Objective-C.
Or I already did is a "trick" using "#objc" key, look at this little explanation: What is #objc attribute, one easy way is just create a helper function that will be visible to your Objective-c class and done like:
#objc func retrieveMainView() -> UIViewController { return MyViewController() }
And you call this from your objective-c class, maybe you need to anotate your swift class with #objc, look at this two reference and you will get the idea and figure out for sure .
In your Objective file i.e. .m file add below import statement:
import "<ProjectName>-Swift.h"
For example your project name is MyProject, so import statement would look like:
import "MyProject-Swift.h"
And call your function like: [mainView runFunction];
I hope this will help. You can also refer one of my answer:
How can I import Swift code to Objective-C?

How to check if class is available or not in Static lib?

I will try to explain my problem.
I am creating two libs home.a & room.a independently. From home lib I have calls to the functions which I implemented in room.a
I am want two use this two libs in one project, the case is I want to keep room.a as optional. If I don't add room.a in project, I am not able to build project.
Error is:
Undefined symbols for architecture
"_RoomViewController", referenced from:
-[ParentViewController openView:] in home.a
Here RoomViewController is class from room.a & ParentViewController is class from home.a
I want to add condition in code home.a to check RoomViewController is present then create a object of RoomViewController.
Please suggest me a way for to do this.
Thanks in advance.
If you want the project to compile without errors, you need to add a header file that declares the RoomViewController class. For instance, write a RoomViewController+Private.h file.
#interface RoomViewController: UIViewController
#end
#interface RoomViewController()
//List of methods you want to use
- (void)methodA;
- (void)methodB;
#end
To check whether you linked the library room.a at runtime, you need to do the following:
if ([RoomViewController class]) {
// class exists
RoomViewController *instance = [[RoomViewController alloc] init];
} else {
// class doesn't exist
}

How do I create a category in Xcode 6 or higher?

I want to create a category on UIColor in my app using Xcode 6. But the thing is that in Xcode 6 there is no Objective-C category file template.
Is there any option to create a category in Xcode 6?
They didn't forget. They just moved it without telling anyone.
Click File -> New -> File
Select Objective-C file under Sources in iOS or Mac OS respectively and Click Next
Now under File Type: choose either Category, Protocol, or Extension
PS. Under File Name: whatever you type here will be either the Category, Protocol, or Extension Name.
To create CategoryBaseClass+CategoryName.m/.h:
File → New → File... or use ⌘N.
Select Objective-C File.
Type in category name, select File Type: Category, and then select the base class.
Complete the flow to create the category.
Here's a visual demonstration:
Xcode6-Beta5 update
The interface has now changed and it's possible to add a Category directly from the New > File window.
See unmircea's answer.
I was surprised myself, and I guess because of Swift they forgot about good old Objective-C.
You have two options:
Create an Objective-C class with the category name, example UIView+Powerups, then manually change the interface to match the one of category. Note that the snippet for the category interface and implementation is still working, so that's extra easy: type #interface-category and #implementation-category.
Import it from Xcode 5! Use this command:
cp -r /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File\ Templates/Cocoa\ Touch/Objective-C\ category.xctemplate /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File\ Templates/Source/
Close and reopen Xcode 6 and you'll find "Objective-C Category" in the wizard for the new file.
There is no predefined template to create category in Xcode 6 beta(for time being),they may add this option later. As a work around you can create a Cocoa Touch Class(its not proper i know but no other way) named UIImage+Additions(ClassName+CategoryName) and override its interface and implementation some thing like
UIImage+Additions.h
#import <UIKit/UIKit.h>
#interface UIImage(Additions)
+(void)testMethod;
#end
UIImage+Additions.m
#import "UIImage+Additions.h"
#implementation UIImage (Additions)
+(void)testMethod
{
}
#end
Edit
This answer was written before finding a way of creating category in the Xcode 6 beta. Check unmircea's answer for the right way of creating category
Extending unmircea's fantastic answer re: how to create a custom category to implement a custom UIColor palette, you could create a category.
Once you've created your category (in this example, it's a category called ColorPalette of class UIColor), you'll have a header and an implementation file.
UIColor+ColorPalette.h
#import <UIKit/UIKit.h>
#interface UIColor (ColorPalette)
// Your custom colors
+ (UIColor *) customRedButtonColor;
+ (UIColor *) customGreenButtonColor;
#end
UIColor+ColorPalette.m
#import "UIColor+ColorPalette.h"
#implementation UIColor (ColorPalette)
// Button Colors
+ (UIColor *) customRedButtonColor {
return [UIColor colorWithRed:178.0/255.0 green:25.0/255.0 blue:0.0/255.0 alpha:1.0];
}
+ (UIColor *) customGreenButtonColor {
return [UIColor colorWithRed:20.0/255.0 green:158.0/255.0 blue:96.0/255.0 alpha:1.0];
}
To use your custom color palette, just import the header into the class where you'd like to implement your custom colors:
#import "UIColor+ColorPalette.h"
and call the color as you would a standard color like redColor, greenColor, or blueColor.
Here's a link to a slightly more in-depth discussion of creating a custom palette.
Additionally, here is a tool to help you select the custom color values
You could just copy the templates you want from an older version of Xcode, I made a shell script for this:https://github.com/cDigger/AddMissingTemplates
You can create "extension" file like NSString+Helper:
1: File → New → File... or use ⌘N.
2: Name NSString+Helper (For example)
3: create the file
4: Remove the code from file
5: add
extension NSString {
}
Done. enjoy coding

how to get value from source code using calabash iOS

I am using calabash cucumber to test my IOS application. The issue is for doing validations and some other operations. I need to get value from the source code. Is it possible? If so, how? Please help me. I have gone through different documentations, but I did not get a proper answer. Thanks in advance.
You can using the calabash-ios query language to call selectors on UIView and its subclasses.
If you have any experience with calabash-ios you probably have been using this feature without realizing it.
# calls the 'text' selector on the labels that match the query
query("label marked:'product description'", :text)
To see if a button is enabled, you could do the following:
# NB the property is 'enabled', but the selector is 'isEnabled
query("button marked:'ask for help'", :isEnabled")
This is described on the calabash-ios wiki:
https://github.com/calabash/calabash-ios/wiki/03.5-Calabash-iOS-Ruby-API
https://github.com/calabash/calabash-ios/wiki/05-Query-syntax
If you want to set custom attributes then one way to do it is to subclass a UI element (such as UIButton and then you can create a property which you can then access like so:
ExampleButton.h:
#interface ExampleButton : UIButton
{
BOOL doingSomething;
}
#property (nonatomic) BOOL isDoingSomething;
ExampleButton.m
#import "ExampleButton.h"
#implementation ExampleButton
... //code possibly here
- (BOOL)isDoingSomething
{
return doingSomething;
}
... //more code possibley here
and then you would have other code that sets doingSomething.
By the way I've only just started out in iOS and Calabash testing so I my objective C is rusty, but I think you can also not have the doingSomething Bool and just have #synthesize isDoingSomething which you can then get and set on the property directly (self.isDoingSomething = true; etc).
In calabash your query would look like this then:
query("view:'ExampleButton' isDoingSomething:1")
or
query("view:'ExampleButton' isDoingSomething:0")
to see if the property is true or false respectively.
This is probably not the only way to do it (it probably isn't even the easiest way to do it but it works).

Resources