LombaXMonday N° 4 – links and resources from the IT World

What is this? Every day I read a lot of articles about computer programming and languages, infrastructures, new technologies, quick tips, trends, architectures and so on. I read them immediately or put in my “read later” list. All my readings helped me to grow and learn new things…so why don’t share them with my readers? For this reason, I decided to publish and share, every monday, a list of the links I came accross during the past days.

Cars and satellite insurance, how to do it (wrong) – link

Andrea Scarpino tell us how he discovered a big security issue in the API provided by his insurance company. With a little bit of reverse engineering on the satellite device provided by the company, he was able to retrieve all the personal informations of all the insurance customers.

Interactice shell scripts with expect command – link

Everyone have tried, at least once, to automate a task writing a simple bash script.
However, how to handle a prompt (for example a password prompt?).

`expect` is a simple program that, with an easy-to-learn syntax permits you to write scripts that expect for a specific prompt before sending something back

MySQL multiple keys and indexes, did you know? by Enomis-

Let’s assume that we have this simple table with an huge quantity of data:

last_name CHAR(30) NOT NULL,
first_name CHAR(30) NOT NULL,
INDEX name (last_name,first_name)

as you can see, we have an index on last_name, first_name

Now, try to query it:

select * from test where first_name=’Fabio’ and last_name=’Lombardo’; // time: 0.5sec

select * from test where last_name=’Lombardo’ and first_name=’Fabio’; // time: 0.01sec

Why this difference?
In MySQL, multiple indexes must be queried in the exact order they have been declared.

More informations:

Multiple-Column Indexes
Avoiding Full Table Scans
Index Hints

That’s all for today’s LombaXMonday, if you liked it, have any question or want to let me know that this article sucks, don’t hesitate to add a comment 🙂

LombaXMonday N° 3 – links and resources from the IT World

What is this? Every day I read a lot of articles about computer programming and languages, infrastructures, new technologies, quick tips, trends, architectures and so on. I read them immediately or put in my “read later” list. All my readings helped me to grow and learn new things…so why don’t share them with my readers? For this reason, I decided to publish and share, every monday, a list of the links I came accross during the past days.

Check the technology behind a website. Is it WordPress? Is it Joomla? – link

Do you want to know what technology power up a website? Tired of inspecting html and headers for clues? Is it WordPress? Is it Joomla? Don’t worry, simply paste the URL in the provided link and enjoy the results

ES6 Overview In 350 Bullet Points – link

A quick and easy recap of all the new features and syntax changes of ES6

Testing code that emits output and native functions in PHP – link

You know (and if you don’t, you should!!! :-), the scope of TDD is to test pieces of software to avoid that it breaks when you change something. It’s very useful because you can simply “run tests” and be sure (if you wrote and structured them correctly) that your changes didn’t affect anything. However, writing good tests is not as simple as it seems, and TDD have various type of tests (for example Unit Tests, Integration Tests, Behavior Tests and so on).

In this link we focus on Unit Tests. In Unit Tests, the difficult part is to decouple things and test all classes as separate, independent entities and test them against their interfaces. You test that, when the contract is respected (when you call their method with the correct dependencies), the class behaves as expected.

To create a successfull Unit Test, it’s very important to test objects only against their interfaces and to mock (see Mockery for php, for example) all their dependencies. With mocks, you create fake dependencies (objects) and check the correct sending/receiving on messages by/to the tested class. You focus mainly on the “communication” between the tested object and the rest of the world. With mocks, you can test a single object without having to integrate all the things together (for example, you can test a query builder without having to use a real database during the test phase).

However, when it comes to native php functions, it seems difficult to create mocked functions and objects. With this guide, you will learn some useful trick and Php language features that will help you in the building of your Unit Tests.


That’s all for today’s LombaXMonday, if you liked it, have any question or want to let me know that this article sucks, don’t hesitate to add a comment 🙂

LombaXMonday N° 2 – links and resources from the IT World

What is this? Every day I read a lot of articles about computer programming and languages, infrastructures, new technologies, quick tips, trends, architectures and so on. I read them immediately or put in my “read later” list. All my readings helped me to grow and learn new things…so why don’t share them with my readers? For this reason, I decided to publish and share, every monday, a list of the links I came accross during the past days.

Monitor your AWS CloudTrail events with Slack and GorillaStack – link

Although it is based on a commercial product, at the provided link you can find a free Slack Bot that will monitor all your AWS CloudTrail events and alert you when something happen. Personally,  I added some triggers to increase the security of our AWS Account: I get an alert when a user login to the AWS Console, when an AWS EC2 Instance is created, rebooted or terminated. It is very easy to install: after adding it to slack, a Cloudformation template that creates everything will be provided, basically an IAM Role, an S3 Bucket, a Lambda function and all the needed CloudTrail configurations are created automatically with a simple step-by-step procedure.

Expose your local development website over a public URL – link

So, you have created a brand new website and you want to show to your customer. But you haven’t yet deployed it to the hosting provider, it’s only available on your local machine via http://localhost. No worries, with ngrok you will be able to share it immediately, via a public URL, writing a simple command. No need to setup a VPN, and event supporting SSL.

Simply install it, write this in your terminal

ngrok http 80

and your site will be publicly available on an URL similar to this: http://xyza335.ngrok.io

What is CQRS? – link and link

Martin Fowler gives us a brief explanation of what is CQRS and its benefits. By splitting the model in two parts (one dedicated to the **query** and the other dedicated to the **command** part) structured applications with complex domains can have huge benefits. But be aware that all comes to a price…don’t over engineer 🙂


That’s all for today’s LombaXMonday, if you liked it, have any question or want to let me know that this article sucks, don’t hesitate to add a comment 🙂

LombaXMonday N° 1 – links and resources from the IT World

Hello there! With this post I want to inaugurate a new section of this blog: LombaXMonday!
One of the things that my wife complains about me is the fact that I don’t read books. However, it doesn’t mean that I don’t read. I’m an avid reader of news of all kinds regarding technology, programming and IT.
Every day I read a lot of links about computer programming and languages, infrastructures, new technologies, quick tips, trends, architectures and so on. I read them immediately or put in my “read later” list. All my readings helped me to grow and learn new things…so why don’t share them with my readers? For this reason, I decided to publish and share, every monday, a list of the links I came accross during the past days.

Ok, now that I explained why, let’s begin with the first LombaXMonday

A roadmap to becoming a web developer in 2017 – link

This article suggests a learning path to become a web developer in 2017. It differentiates between frontend, backend and devops. Despite its content can be debatable, you can give it a look even if you are an experienced web developer, so you can evaluate if you lacks of some nice-to-have skills and start learning again.

Composition over Inheritance – link

Ok, many of you knows, I don’t like Javascript and the frontend in general 🙂
However, I think that the concepts explained in this video are useful also for backend developers and can be easily ported to the PHP ecosystem. Learn how to create your objects using Composition and avoid the headaches of the classic Inheritance.

The art of command line (Italian) – link

Do you think to be a master of the command line? Do you think that Bash has no secrets for you? You are wrong. For sure. Did you know the ctrl-r shortcut? And ctrl-w?
Spend a little time studying the linked article and learn how to master – REALLY – the command line. You will never stop to use these new tips.
Note: The article is in Italian


That’s all for the first LombaXMonday, if you liked it, have any question or want to let me know that this article sucks, don’t hesitate to add a comment 🙂

Soluzioni per Live Streaming

NB: questo articolo è stato scritto velocemente, ma vuole riassumere diverse giornate di analisi delle tecnologie e del mercato.
Se non capite qualche parte o volete maggiori dettagli, contattatemi pure. Con il tempo aggiungerò dettagli.

Parliamo delle tecnologie di Live Streaming, sistemi per trasmettere live e a migliaia di persone, in tempo reale, un flusso video.

Ho effettuato questa analisi per esigenza aziendale.

Nel nostro caso l’esigenza era duplice: in alcuni casi, trasmettere un evento live a qualche migliaio di persone (troupe video presente). In altri casi, effettuare brevi trasmissioni da parte di singoli (responsabili dei ns clienti), non assistiti (quindi tecnologia *semplice*), a diverse centinaia di persone.

Prima di entrare nei dettagli, è bene analizzare le varie parti dello stack di cui un Live Streaming si compone


Fase di ripresa e acquisizione (transcoding):

Questa prima fase è, banalmente, il momento in cui effettuate le riprese. Dato che parliamo di “Live”, il flusso video deve essere catturato e trasmesso in tempo reale. Qualunque sia il vostro sistema video per le riprese (telecamere professionali, webcam, cellulari), il vostro scopo è quello di far arrivare un segnale video a un pc, che dovrà poi codificarlo secondo uno standard e trasmetterlo a un server.

Esistono inoltre delle App per cellulari che facilitano il compito: acquisiscono il segnale dalla telecamera on-board, lo impacchettano e spediscono direttamente secondo lo standard.
Per la fase di trasmissione, ci focalizziamo sullo standard RTMP ( https://it.wikipedia.org/wiki/Real_Time_Messaging_Protocol ). E’ possibile usare anche RTSP ( https://it.wikipedia.org/wiki/Real_Time_Streaming_Protocol ). Non ho analizzato altri standard.


Fase di creazione dello stream:

Questa fase viene effettuata da un server dedicato, quello a cui state inviando il flusso RTMP. Il server riceve il flusso video, lo transcodifica ulteriormente (producendo i chunk video a varie risoluzioni/bitrate e formati) e li rende disponibili per la fruizione.


Fase di distribuzione:

il server che crea lo stream non ha una banda sufficiente a servire tutti i vostri utenti. Per questo motivo non viene esposto direttamente su internet, viene piuttosto utilizzato un sistema di distribuzione (tipicamente una CDN https://it.wikipedia.org/wiki/Content_Delivery_Network ). Quindi il vostro server di encoding carica lo stream definitivo sulla CDN rendendolo disponibile agli utenti


Fase di fruizione:

i vostri utenti tipicamente (ma non necessariamente) si collegano al sito internet dove ospitate il video. In questo sito è inserito un player, che tipicamente è un’applicazione Flash o un player HTML5. All’avvio della fruizione, il player contatta l’url CDN e inizia a scaricare il video. Anche in questa fase, esistono diversi standard e tecnologie: quelle che ho analizzato sono sempre RTMP (può essere usato indipendentemente sia per la fase di upload che per quella di download), oppure HLS (standard più recente)
Una variabile importante, se lavorate su siti internet, è la possibilità di vedere il video direttamente dalla pagina che lo ospita (embed), senza farlo aprire in un popup. Altra variabile importante è la possibilità di scegliere diversi bitrate manualmente o in automatico (a seconda della banda disponibile lato client). In questo caso parleremo di JWPlayer ( https://www.jwplayer.com ) che gestisce tutte queste casistiche.


Ora che abbiamo parlato delle varie parti dello stack, passiamo a un po’ di nomi, tecnologie e consigli. Iniziamo con il fai da te, se volete dei servizi che coprono tutta questa parte in pieno outsourcing staltate pure questa parte. Anche se vi consiglio caldamente di leggerla per imparare qualcosa 😉

Per la fase di ripresa
possiamo utilizzare telecamere professionali con uscita analogica o digitale, acquisendo poi il segnale con una scheda di acquisizione tipo questa:


Oppure è possibile usare direttamente la webcam.
In questa fase vogliamo encodare il segnale in ingresso in un flusso RTMP da inviare al nostro server.
Esistono diversi software per questa fase, io personalmente consiglio Wirecast: http://www.telestream.net/wirecast/overview.htm

Il setup è semplice, una volta acquisito correttamente il segnale video dovrete impostare i dati del server RTMP che riceverà il flusso video. Tipicamente saranno un indirizzo ip, uno username e una password.

Se volete un setup più semplice per eseguire le prime prove, scaricate un’app all-in-one, su iPhone posso consigliare questa: https://itunes.apple.com/it/app/broadcast-me/id491982406?mt=8 (esiste anche una versione RTSP, ma in questo caso noi vogliamo usare la versione RTMP)

per la fase di creazione dello stream:
il nostro server riceverà lo stream RTMP e dovrà encodarlo secondo i diversi standard che vogliamo poi fornire al nostro player (passando per la CDN).
Gli strumenti consigliati in questa fase sono due:
*Apple Media Encoder e altri software a supporto (ostico ma sicuramente il più configurabile)
*Adobe Media Encoder (non l’ho usato personalmente)

In questa fase consiglio di creare uno stream HLS ( https://en.wikipedia.org/wiki/HTTP_Live_Streaming ), in particolare l’encoder prende il video live e lo segmenta in piccoli pezzi (da 10 secondi ciascuno), in tante risoluzioni diverse, e li carica sulla CDN. L’encoder mantiene aggiornato un file di index (file .m3u8) che viene usato dal player per referenziare correttamente i file man mano che vengono aggiunti.

per la fase di distribuzione:

in questa fase ci servirà appoggiarci a una CDN e depositare i file sullo storage ad essa associato. In alcuni casi sarà la CDN a recuperare i file (tramite una http request a un vostro server, anche direttamente al media server se volete).
Poco da dire, facciamo qualche nome:
• Akamai
• CDNetworks
• Edgecast
• Limelight

ce ne sono tante altre, ma queste hanno un particolare: possono ricevere (e poi redistribuire) uno stream RTMP direttamente, senza la necessità di un encoder intermedio come da punto due. In questa guida non copriamo questa casistica, ma è giusto saperlo. Io comunque vi sconsiglio questo setup, in quanto RTMP è superato per la parte di distribuzione, in favore del più recente e flessibile HLS.

per la fase di fruizione:

anche in questo caso poche parole, vi consiglio di usare JWPlayer che ha già il supporto nativo per HLS e un player HTML5 nativo (o flash in fallback nel caso il vostro device non supporti html5).
A questa pagina trovate dettagli per il setup: https://support.jwplayer.com/customer/portal/articles/1430218-using-hls-streaming


Una volta parlato del fai da te, è giusto dire che non è sempre corretto reinventare la ruota. Esistono servizi che, a fronte di un costo mensile, coprono in tutto o in parte le varie parti dello stack.

Il più completo che ho trovato è Ustream: copre l’intero stack. Fornisce un’interfaccia web per l’acquisizione, con la possibilità di acquisire dalla webcam direttamente all’interno del browser e, nella versione pro, fornisce il codice embed per il player. Insomma, copre tutte e 4 le fasi in maniera molto semplice.
Si possono utilizzare telecamere professionali ed effettuare lo streaming anche tramite un software dedicato, Ustream producer. Inoltre vi permette di inviare il vostro flusso RTMP direttamente (se volete usare un’app o uno streamer hardware).

Altri nomi di cui scriverò più avanti: DaCast, StreamZilla.


Vi lascio alcuni link utili:
Bibbia dello streaming video by Apple

Strumenti server Apple

Alcuni transcoder:

Alcune app per streaming da iPhone

Swift and pointers, news in Beta 3 and 4

In this post: https://www.lombax.it/?p=302 you have learned how to play with pointers in Swift.
Things have changed in latest betas, and more researches came in a better, shorter and more concise code. In short:

– CMutablePointer has been removed
– Code like this:

// swift side
 var opaque = COpaquePointer.null() // create a new opaque pointer pointing to null
 var string = Unmanaged.fromOpaque(opaque).takeRetainedValue()
 // this prints pippo pluto

has been revealed to be not safe:
There are circumstances in which Swift does not give you the original address of the variable, but instead gives you an address of a temporary variable that will be assigned back to the original once the call is over. That means you can’t count on the address of opaque being valid after the call to TestClass.writeString(&opaque). So, starting at the line ” var string = Unmanaged.fromOpaque(opaque).takeRetainedValue()”, the code can fail

Now, let’s say that we want to create a C/Objc function that takes all kind of objects and values (simply, it can receive a void** pointer), and pass a value back to Swift.
In this example we will pass in an NSString, changing its value in Objc and then reading it back in Swift. There are better methods to do this kind of things with NSString, passing a void** is used mainly to manage buffers, but in the example we’ll use an NSString for leaving things simple.

The Objc method is this

// with this objc function I completely replace the pointer
 + (void)writeString:(void **)var
     NSString *oldString = CFBridgingRelease(*var); // since we are transferring opaque pointers, ARC can't help us. So, we have to release the old string before replacing it, otherwise it will leak 
     NSString *aString = [[NSString alloc] initWithFormat:@"pippo %@", @"pluto"];
     *var = (void *)CFBridgingRetain(aString); // Since aString will be released by ARC when the method ends, we need to send an additional retain. This retain is balanced by ARC on the Swift side

Now, on the Swift part, the code is more simple than the previous version:

var aString : NSString = NSString(string: "test") // create the string
withUnsafePointer(&aString) { (arg: UnsafePointer) -> () in
     var aPtr = reinterpretCast(arg) as UnsafePointer<UnsafePointer<()>>
println(aString) // this will print "pippo pluto"

Some things to know

1) withUnsafePointer is a function that takes: an inout parameter (&parameterName) and a closure, where an arg (of type UnsafePointer) is passed in.
It can be written even in this way

withUnsafePointer(&aString, { … closure ….})

However, Swift give you a convenient way to put the closure after the parentheses. For this, look at the “Trailing Closures” chapter in the Swift Programming Language Book

2) reinterpretCast() permits to easily change the type of a pointer to another. Since Swift types are all reference types, and we want to obtain the void** pointer:
– when we pass in &aString we obtain a pointer to a Swift Reference type to an NSString, so a pointer to a pointer, and so a void**. We need only to make it “more generic”, and we to this casting to UnsafePointer<UnsafePointer<()>>
() is a TypeAlias to Void, so aPtr will be a pointer to a void pointer

Swift and mach_override, an (unsafe) alternative to objc method swizzling

Objective-c method dispatching is dynamic. This means that, when you call for example an instance method, the objc runtime “searches” the address of the underlying function (*) in a dynamical way. It uses objc_msgSend() for the message dispatching.
I don’t want to go into details here, there is a lot of documentation on the web about it.

(*) every objective c method is a plain c function that takes a _cmd (selector) as the first parameter, and self as the second parameters, then a variable list of parameters.

One advantage of the dynamic dispatching is that you can add classes, variables and methods at runtime.

This is not possible with Swift, since it is based on a vtable approach (*)
(*) This is true for plain swift classes, since classes the inherith from objective-c uses, for compatibility reasons, the dynamic dispatching and the runtime of objective c. In some cases, even the vtable is not used: the function calls are inlined

This means that there is no dynamic search of the functions/methods at runtime, their addresses are statically written at compile time inside the vtable, and it’s not possible to change them at runtime.

If you try to look at the symbols of a Swift compiled app, you will see something like this (OverrideTest is a binary name that I’m analyzing):

LombaXAir:MacOS Lombardo$ nm OverrideTest
 00000001000071e0 t _Mx_mem_size
 00000001000071c0 t _Mx_reg_size
 U _NSApplicationMain
 0000000100010f90 S _OBJC_CLASS_$__TtC12OverrideTest11AppDelegate
 0000000100011070 D _OBJC_METACLASS_$__TtC12OverrideTest11AppDelegate
 0000000100011010 s _OBJC_METACLASS_$___ARCLite__
 U __Block_copy
 0000000100004480 t __OSSwapInt32
 0000000100004490 t __OSSwapInt64
 0000000100001700 T __TF12OverrideTest16doSomethingSwiftFT_T_
 0000000100001aa0 T __TF12OverrideTest26doSomethingSwiftOverriddenFT_T_
 0000000100001820 T __TFC12OverrideTest11AppDelegate13aTestFunctionfS0_FT_T_
 00000001000018b0 T __TFC12OverrideTest11AppDelegate18anOverrideFunctionfS0_FT_T_
 00000001000017a0 T __TFC12OverrideTest11AppDelegate24applicationWillTerminatefS0_FGSqCSo14NSNotification_T_
 0000000100001690 T __TFC12OverrideTest11AppDelegate29applicationDidFinishLaunchingfS0_FGSqCSo14NSNotification_T_
 0000000100001a40 T __TFC12OverrideTest11AppDelegateCfMS0_FT_S0_
 0000000100001940 T __TFC12OverrideTest11AppDelegateD
 00000001000019b0 T __TFC12OverrideTest11AppDelegatecfMS0_FT_S0_
 0000000100001590 T __TFC12OverrideTest11AppDelegateg6windowXwGSQCSo8NSWindow_
 0000000100001640 T __TFC12OverrideTest11AppDelegates6windowXwGSQCSo8NSWindow_
 U __TFSS37_convertFromBuiltinUTF16StringLiteralfMSSFTBp17numberOfCodeUnitsBw_SS
... continue

Symbols like


are plain function/method names, mangled ( see Name Mangling ), that can be called by a C/ObjectiveC function without problems.
You can call them with the static address, or searching by name through dlsym (see this post: Swift Performselector alternative )

So, returning to the original topic, how to do something like method swizzling in Swift?

There is an alternative called mach_override. This is not safe and I discourage you to include it in a shipping application, however it’s possible.

mach_override “simply” add a jmp inside the assembly of the original function, making it jump to another custom function, and then jump back to the original one, if you want.

More info about mach_override Here (Github) and Here an high level explanation on how it works

After including mach_override in your project, you can override a method implementation with a c function like this:

void override()
    void (*landing)(void * par);
    void (*originalFunctionAddress)(void * par);
    const void (*overrideFunctionAddress)(void * par);
   *(void **) (&originalFunctionAddress) = dlsym(RTLD_DEFAULT, "_TF15OverrideTestIOS16doSomethingSwiftFT_T_");
   *(void **) (&overrideFunctionAddress) = dlsym(RTLD_DEFAULT, "_TF15OverrideTestIOS26doSomethingSwiftOverriddenFT_T_");
   mach_override_ptr(originalFunctionAddress, overrideFunctionAddress, (void**)&landing);

Method names are taken with “nm” from command line, but you can try to mangle it at runtime since the mangling syntax has been reverse engineered (an example by Evan Swick Here )

the “par” parameter is because every swift instance method underlying function, takes “self” as the last parameter.

To see this code in action, download a working example (for mac) from my GitHub account

How do I specify that a non-generic Swift type should comply to a protocol?

From this Stackoverflow Question

Given this example method in Objective C:

- (void)addFilter:(GPUImageOutput <GPUImageInput>*)newFilter;

How to rewrite it in Swift, saying that the input parameter is of Class GPUImageOutput (or subclass), and, at the same time, conforms to the protocol GPUImageInput ?

In Swift, the solution comes from Generics:

Given these example declarations of protocol, main class and subclass:

protocol ExampleProtocol {
    func printTest() // classes that implements this protocol must have this method
// an empty test class
class ATestClass
// a child class that implements the protocol
 class ATestClassChild : ATestClass, ExampleProtocol
     func printTest()

Now, you want to define a method that takes an input parameters of type ATestClass (or a child) that conforms to the protocol ExampleProtocol. Write the method declaration like this:

func addFilter<T where T: ATestClass, T: ExampleProtocol>(newFilter: T)

The original method, redefined in Swift, should be

func addFilter<T where T:GPUImageOutput, T:GPUImageInput>(newFilter:T!)
    // ...

A quick tour into Swift Pointers

From: Stackoverflow Question

Swift “hides” pointers, but they still exists under the hood. (because the runtime needs it, and for compatibility reasons with Objc and C)

There are few things to know however, but first how to print the memory address of a Swift String?

var aString : String = "THIS IS A STRING"
 NSLog("%p", aString.core._baseAddress) // _baseAddress is a COpaquePointer
 // example printed address 0x100006db0

This prints the memory address of the string, if you open XCode -> Debug Workflow -> View Memory and go to the printed address, you will see the raw data of the string. Since this is a string literal, this is a memory address inside the storage of the binary (not stack or heap).

However, if you do

var aString : String = "THIS IS A STRING" + "This is another String"
 NSLog("%p", aString.core._baseAddress)
// example printed address 0x103f30020

This will be on the stack, because the string is created at runtime

NOTE: .core._baseAddress is not documented, I found it looking in the variable inspector, and it may be hidden in the future

_baseAddress is not available on all types, here another example with a CInt

var testNumber : CInt = 289

Where takesInt is a C helper function like this

void takesInt(int *intptr)
    printf("%p", intptr);

On the Swift side, this function is takesInt(intptr: CMutablePointer), so it takes a CMutablePointer to a CInt, and you can obtain it with &varname

The function prints 0x7fff5fbfed98, an at this memory address you will find 289 (in hexadecimal notation). You can change its content with *intptr = 123456

Now, some other things to know.

String, in swift, is a primitive type, not an object.
CInt is a Swift type mapped to the C int Type.
If you want the memory address of an object, you have to do something different.
Swift has some Pointer Types that can be used when interacting with C, and you can read about them here: Swift Pointer Types
Moreover, you can understand more about them exploring their declaration (cmd+click on the type), to understand how to convert a type of pointer into another

var aString : NSString = "This is a string" // create an NSString
 var anUnmanaged = Unmanaged.passUnretained(aString) // take an unmanaged pointer
 var opaque : COpaquePointer = anUnmanaged.toOpaque() // convert it to a COpaquePointer
 var mut : CMutablePointer = &opaque // this is a CMutablePointer
printptr(mut) // pass the pointer to an helper function written in C

printptr is a C helper function I created, with this implementation

void printptr(void ** ptr)
    printf("%p", *ptr);

Again, an example of the address printed: 0x6000000530b0 , and if you go through memory inspector you will find your NSString

One thing you can do with pointers in Swift (this can even be done with inout parameters)

func playWithPointer (stringa :AutoreleasingUnsafePointer)
    stringa.memory = "String Updated";
var testString : NSString = "test string"

Or, interacting with Objc / c

// objc side
 + (void)writeString:(void **)var
    NSMutableString *aString = [[NSMutableString alloc] initWithFormat:@"pippo %@", @"pluto"];
    *var = (void *)CFBridgingRetain(aString); // Retain!
// swift side
 var opaque = COpaquePointer.null() // create a new opaque pointer pointing to null
 var string = Unmanaged.fromOpaque(opaque).takeRetainedValue()
 // this prints pippo pluto

Subclass UIApplication in Swift

From Stackoverflow

In Objective C it was simple: it was sufficient to update the main.m file and change the UIApplicationMain() parameters

return UIApplicationMain(argc, argv, NSStringFromClass([CustomUIApplication class]), NSStringFromClass([AppDelegate class]));

But in swift there is no main.m file, since the guide says

“Code written at global scope is used as the entry point for the program, so you don’t need a main function.”

So, how to subclass UIApplication in swift?? Any suggestion?


Ok, I’ve found the solution First, I’ve noticed that, at the top of the AppDelegate.swift file, there is this line


Since this line is outside any scope (it’s at file level), it’s executed immediately, and I assume that the compiler translate it in a standard main function.

So, I did this, starting from a new Swift-Only application:

– commented out @UIApplicationMain
– added a main.swift file like this (FLApplication is my subclass).

IMPORTANT the file MUST BE NAMED main.swift, since top level statements are not supported on other files! You can’t add the UIApplicationMain() call inside any other file, otherwise you’ll receive this error:
“Expressions are not allowed at the top level”

This is the main.swift file .

import Foundation
import UIKit

UIApplicationMain(C_ARGC, C_ARGV, NSStringFromClass(FLApplication), NSStringFromClass(AppDelegate))

create a swift file for the UIApplication subclass, FLApplication.swift, with this code:

import UIKit
import Foundation

class FLApplication: UIApplication
    override func sendEvent(event: UIEvent!)
        println("send event") // this is an example
        // ... dispatch the message...

now, UIApplication is correctly subclassed and you’ll see the “send event” messages in the log