IOS , Reactnative , Android Developer, Tech, LLVM Compiler Enthusiast

Interoperability With Both World

|

Intro

Swift designed to pervoide seamless compatibility with Cocoa and objective-c . you can use objective c api in swift as well . its a powerful tool to integrate into you workflow.

Start from Import

import Foundation

This Statement will import statement access all of Foundation’s classes, protocols, methods, properties, and constants. The Process of importing can have following effects

  • objective-c type to their equivalent in swift like id to Any
  • Objective-C core types to their alternatives in Swift, like NSString to String
  • Objective-C concepts to matching concepts in Swift, like pointers to optionals

NOTE

You cannot import C++ code directly into Swift. Instead, create an Objective-C or C wrapper for C++ code

Alongside these Swift modules are generated Objective-C headers.These headers vend the APIs that can be mapped back to Objective-C. Some Swift APIs do not map back to Objective-C because they leverage language features that are not available in Objective-C.

initialization

obj-c initialisation begin with init or initwith: When an Objective-C initializer is imported by Swift, the init prefix becomes an init keyword to indicate swift initializer. objc initializer

-(instancetype)init;
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;

swift equivalent swift initializer

init(){}
init(frame:CGRect, style: UITableViewStyle){}

if you go through these 2 styles of syntax you will find don’t need to call alloc Swift handles this for you. Notice also that init doesn’t appear anywhere when calling the Swift-style initializer.

Example

objc UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; swift let myTableView: UITableView = UITableView(frame: .zero, style: .grouped)

Failable Initialization

In objc initalizer directly return the object they initialize. or when initialiser failed an it return nil. in swift the patten built into a language feature called failable initialization.

if you want to design you own you can choose You can indicate whether initializers in your own Objective-C classes can fail using nullability annotations .

nullability annotation

  1. indicate whether objc/c pointer can be nil
  2. better Communicates the intent of apis
  3. allow better static checking
  4. improves usages in apis in swift
  • Types declared to be nonnullable, either with a _Nonnull annotation or in an audited region, are imported by Swift as a nonoptional.
  • Types declared to be nullable with a _Nullable annotation, are imported by Swift as an optional.
  • Types declared without a nullability annotation are imported by Swift as an implicitly unwrapped optional

[tableView deselectRowAtIndexPath: nil animated: false]; these api is you pass nil new warnings for nonnull parameters.

example

UIImage(contentsOfFile:) initialize can fail to initialize a UIImage object if an image file doesn’t exist at the provided path. You can use optional binding to unwrap the result of a failable initializer if initialization is successful

Accessing Properties

Objective-C property  declarations using the @property syntax are imported as Swift properties in the following way:

  • Properties with the nullability property attributes (nonnull, nullable, and null_resettable) are imported as Swift properties with optional or nonoptional type
  • Properties with the readonly property attribute are imported as Swift computed properties with a getter ({ get })
  • Properties with the weak property attribute are imported as Swift properties marked with the weak keyword (weak var)
  • Properties with an ownership property attribute other than weak (that is, assign, copy, strong, or unsafe_unretained) are imported as Swift properties with the appropriate storage
  • Properties with the class property attribute are imported as Swift type properties
  • Atomicity property attributes (atomic and nonatomic) are not reflected in the corresponding Swift property declaration, but the atomicity guarantees of the Objective-C implementation still hold when the imported property is accessed from Swift
  • Accessor property attributes (getter= and setter=) are ignored by Swift

    Working With Methods

    obj-c method calls like this [myTableView insertSubview:mySubview atIndex:2]; in swift these calls are like this myTableView.insertSubview(mySubview, at: 2) If you’re calling a method with no arguments, you must still include parentheses. myTableView.layoutIfNeeded()

ID Compatibility

  1. the Obj-c id type is imported by swift as Any.
  2. the compiler introduces a universal bridging conversion operation when a Swift value or object is passed into Objective-C as an id parameter.
  3. When id values are imported into Swift as Any, the runtime automatically handles bridging back to either class references or Swift value types.

DownCasting Any

Any means any type downcast any type is not guaranteed to succesed . so we have to use (as?) or if certain about of the object you force downcast the object (as!) instead . if force Downcast the object fails a runtime error is trigger . —–

Want to see something else added? Open an issue.

Introducing Lanyon

|

Lanyon is an unassuming Jekyll theme that places content first by tucking away navigation in a hidden drawer. It’s based on Poole, the Jekyll butler.

Built on Poole

Poole is the Jekyll Butler, serving as an upstanding and effective foundation for Jekyll themes by @mdo. Poole, and every theme built on it (like Lanyon here) includes the following:

  • Complete Jekyll setup included (layouts, config, 404, RSS feed, posts, and example page)
  • Mobile friendly design and development
  • Easily scalable text and component sizing with rem units in the CSS
  • Support for a wide gamut of HTML elements
  • Related posts (time-based, because Jekyll) below each post
  • Syntax highlighting, courtesy Pygments (the Python-based code snippet highlighter)

Lanyon features

In addition to the features of Poole, Lanyon adds the following:

  • Toggleable sliding sidebar (built with only CSS) via link in top corner
  • Sidebar includes support for textual modules and a dynamically generated navigation with active link support
  • Two orientations for content and sidebar, default (left sidebar) and reverse (right sidebar), available via <body> classes
  • Eight optional color schemes, available via <body> classes

Head to the readme to learn more.

Browser support

Lanyon is by preference a forward-thinking project. In addition to the latest versions of Chrome, Safari (mobile and desktop), and Firefox, it is only compatible with Internet Explorer 9 and above.

Download

Lanyon is developed on and hosted with GitHub. Head to the GitHub repository for downloads, bug reports, and features requests.

Thanks!

Example content

|
Howdy! This is an example blog post that shows several types of HTML content supported in this theme.

Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Sed posuere consectetur est at lobortis. Cras mattis consectetur purus sit amet fermentum.

Curabitur blandit tempus porttitor. Nullam quis risus eget urna mollis ornare vel eu leo. Nullam id dolor id nibh ultricies vehicula ut id elit.

Etiam porta sem malesuada magna mollis euismod. Cras mattis consectetur purus sit amet fermentum. Aenean lacinia bibendum nulla sed consectetur.

Inline HTML elements

HTML defines a long list of available inline tags, a complete list of which can be found on the Mozilla Developer Network.

  • To bold text, use <strong>.
  • To italicize text, use <em>.
  • Abbreviations, like HTML should use <abbr>, with an optional title attribute for the full phrase.
  • Citations, like — Mark otto, should use <cite>.
  • Deleted text should use <del> and inserted text should use <ins>.
  • Superscript text uses <sup> and subscript text uses <sub>.

Most of these elements are styled by browsers with few modifications on our part.

Heading

Vivamus sagittis lacus vel augue rutrum faucibus dolor auctor. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.

Code

Cum sociis natoque penatibus et magnis dis code element montes, nascetur ridiculus mus.

// Example can be run directly in your JavaScript console


// Create a function that takes two arguments and returns the sum of those arguments

var adder = new Function("a", "b", "return a + b");

// Call the function

adder(2, 6);
// > 8

Aenean lacinia bibendum nulla sed consectetur. Etiam porta sem malesuada magna mollis euismod. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa.

Lists

Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean lacinia bibendum nulla sed consectetur. Etiam porta sem malesuada magna mollis euismod. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.

  • Praesent commodo cursus magna, vel scelerisque nisl consectetur et.
  • Donec id elit non mi porta gravida at eget metus.
  • Nulla vitae elit libero, a pharetra augue.

Donec ullamcorper nulla non metus auctor fringilla. Nulla vitae elit libero, a pharetra augue.

  1. Vestibulum id ligula porta felis euismod semper.
  2. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
  3. Maecenas sed diam eget risus varius blandit sit amet non magna.

Cras mattis consectetur purus sit amet fermentum. Sed posuere consectetur est at lobortis.

HyperText Markup Language (HTML)
The language used to describe and define the content of a Web page
Cascading Style Sheets (CSS)
Used to describe the appearance of Web content
JavaScript (JS)
The programming language used to build advanced Web sites and applications

Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. Nullam quis risus eget urna mollis ornare vel eu leo.

Tables

Aenean lacinia bibendum nulla sed consectetur. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Name Upvotes Downvotes
Totals 21 23
Alice 10 11
Bob 4 3
Charlie 7 9

Nullam id dolor id nibh ultricies vehicula ut id elit. Sed posuere consectetur est at lobortis. Nullam quis risus eget urna mollis ornare vel eu leo.


Want to see something else added? Open an issue.

What's Jekyll?

|

Jekyll is a static site generator, an open-source tool for creating simple yet powerful websites of all shapes and sizes. From the project’s readme:

Jekyll is a simple, blog aware, static site generator. It takes a template directory […] and spits out a complete, static website suitable for serving with Apache or your favorite web server. This is also the engine behind GitHub Pages, which you can use to host your project’s page or blog right here from GitHub.

It’s an immensely useful tool and one we encourage you to use here with Lanyon.

Find out more by visiting the project on GitHub.