[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

[coco8] Wrapping iOS native Classes

In order to write S8 applications for iOS entirely in Smalltalk, We use to have Smalltalk wrappers for every single iOS native class (Actually, every class we need).

We will create here, as an example, a Smalltalk wrapper for UITextField Objective C native class.
UITextField is part of UIKit Framework.

NoteMost of UIKit framework Smalltalk wrappers are in UIKit.st.
See our media swiki for iOS SDK.
Read more about "Implementing wrappers with S8".

Let's add UITextField!.
In order to create the wrapper, we need to know some information about UITextField Objective C class (Like properties, variables, methods...).
So, the best place to find this info is the Apple official documentation: UITextField class reference.
You can also explore the UITextField.h file using xcode.

Locate UITextField (Objective-C native class) in the Class Hierarchy
Uploaded Image: UITextFieldIdHierarchy.jpg

So We start with this chunk:
 ! UITextField methodsFor: #accessing !
text
        " Return the text of the receiver. "

        ^self handle basicAt: #text! !


Fortunately, We don't need to write every single accessor explicitly.
There are some helper methods for creating these wrappers faster:
For example:
UITextField buildAccessors: #(
	text
	placeholder
	textAlignment
	typingAttributes
)!

creates automatically all the setters and getters for text, placeholder, textAlignment and typingAttributes in UITextField wrapper

And:
UITextField buildTypedAccessors: #(
	#( attributedText #NSAttributedString )
	#( delegate #NSObject )
	#( font #NSFont )
	#( inputAccessoryView #UIView )
	#( inputView #UIView )
	#( textColor #UIColor )
)!

creates automatically all the setters and getters for properties whose types are other Wrapper classes

And:
UITextField buildBooleanAccessors: #(
	allowsEditingTextAttributes
	adjustsFontSizeToFitWidth
	clearsOnBeginEditing
	clearsOnInsertion
	editable
        selectable
)!

creates automatically all the setters and getters for properties whose types are BOOL

And so on...
And buildGetters:, buildSetters:, buildTypedGetters:, buildTypedSetters:, buildBooleanGetters:, buildBooleanSetters:, buildNumberGetters:, buildNumberSetters:, and also
buildFunctions
for implementing methods whose implementation consists in the invocation of the respective method in the native handle object

UITextField Wrapper Code


UIControl subclass: #UITextField!
UITextField buildAccessors: #(
	text
	placeholder
	textAlignment
	typingAttributes
)!
UITextField buildBooleanAccessors: #(
	allowsEditingTextAttributes
	adjustsFontSizeToFitWidth
	clearsOnBeginEditing
	clearsOnInsertion
	editable
        selectable
)!
UITextField buildBooleanGetters: #(
	editing
)!
UITextField buildTypedAccessors: #(
	#( attributedText #NSAttributedString )
	#( delegate #NSObject )
	#( font #NSFont )
	#( inputAccessoryView #UIView )
	#( inputView #UIView )
	#( textColor #UIColor )
)!

UITextField buildFunctions: #(
	#( #drawTextInRect: #( #( rect handle ) ) )
)! !

Note:

It is not mandatory to wrap all the methods of the original native class. You can implement/wrap only the ones you will use directly from your Smalltalk code.
There are some native functions that require out arguments (variables to contain data after the function has been evaluated).
There is a page on the topic here