Moving out of tumblr
I decided to move my whole blog to github pages.
Please check my new blog location for the old and new contents: http://marcinczenko.github.io.

祝日 / Permanent Vacation
he wasn't even looking at me and he found me
noise dept.
almost home
Three Goblin Art
trying on a metaphor
todays bird
dirt enthusiast
🪼
cherry valley forever
Claire Keane
ojovivo
Peter Solarz
Keni

Kiana Khansmith

izzy's playlists!

blake kathryn
No title available
Jules of Nature
tumblr dot com

seen from United States

seen from Malaysia
seen from Czechia
seen from United States

seen from Oman
seen from United States

seen from Canada

seen from Malaysia
seen from South Africa

seen from Malaysia
seen from Malaysia

seen from Malaysia

seen from Oman
seen from United States
seen from United States
seen from United States

seen from United States
seen from United States

seen from United States

seen from United States
@redgreenrefactor-eu
Moving out of tumblr
I decided to move my whole blog to github pages.
Please check my new blog location for the old and new contents: http://marcinczenko.github.io.
Distributed persistence with Swift and CoreData
A component-based architecture gives us high level of isolation and independence of the underlying components, but at the same time exibits potential risk of duplication. How would a component-based architecture work for iOS apps, especially when persistance is the key. Let's experiment...
Let me create a framerwork in which I will have seprate components. I want my components to be possibly independent from each other, but at the same time I want to limit duplication. And trying to push the idea to the extreme, if a component needs to store anything in CoreData (we will use CoreData in this example, but you can easily abstract it away and use different storage options), I let the component to have its own model and its own sqlite file - in other words rather than sharing one CoreData stack among all the components, each component maintains its own instance of the CoreData stack. I am not sure how robust this architecture would be, and how well will it scale in terms of perfomance, but the possibility of throwing a component away at any moment without worrying about other component is very appealing and may be a good option in some applications.
Every type that is supposed to be stored in CoreData has its domain level equivalent. CoreData's managed objects, actually everything that's related to CoreData can only occur in the CoreData stack itself and in a wrapper that encapsulates low-level CoreData operations and translates between managed objects and the corresponding domain types.
Above the CoreData wrapper, there is a data store that provides high level persistance interface and enforces data integrity.
The CoreData wrapper and the data store provide a uniform set of operations that can be reused among components. Each component will have its own instances of the CoreData stack, the CoreData wrapper, and a data store, but I want them all to share the same source code.
Domain objects
All our domain objects have to conform to the following protocol:
protocol EPQuantity { associatedtype EPQuantityType func getValue() -> EQQuantityType }
The associatedtype is a placeholder for the actuall type that will be provided by the type conforming to the protocol. Our protocol requires that a type conforming to it must have a method getValue returing a value of a type that is specific to the given domain object. This makes the protocol a good candidate for many types as long they can provide a value.
Now, lets take two example domain level value types conforming to the EPQuantity protocol:
struct EPAcceleration: EPQuantity, CustomStringConvertible { let acceleration: Double var description: String { return "EPAcceleration(acceleration: \(acceleration))" } func getValue() -> Double { return acceleration } } struct EPSpeed: EPQuantity, CustomStringConvertible { let speed: Int var description: String { return "EPSpeed(speed: \(speed))" } func getValue() -> Int { return speed } }
What is important to see is that both types represents quantities of different types: we have an Int for the speed, but Double for acceleration.
Data store
Now we want to be able to store the domain objects in a data store. We may be tempted to start with the following implementation:
class EPDataStore { func store(item: EPQuantity) func getAll() -> EPQuantity }
The compiler, however, will give us the following compile-time error: Protocol 'EPQuantity' can only be used as a generic constraint because it has Self or associated type requirements.
To fix this problem we can either do:
class EPDataStore { func store<T: EPQuantity>(item: T) { } func getAll<T: EPQuantity>() -> [T]? { return nil } }
or just:
class EPDataStore<T: EPQuantity> { func store(item: T) { } func getAll() -> [T]? { return nil } }
The problem we may have with this implementation is that we may want to be slightly more specific about which types should be allowed to enter the store. For instance, EPSpeed and EPAcceleration being domain equivalent of some managed objects, will ultimately need to match the corresponding types of the attributes in the manged objects. We can immediatelly think of Int, Double, NSTimeInterval, String, NSDate, NSNumber and perhapes a couple of more options. For now, to support EPSpeed and EPAcceleration, we only need to support Int and Double. How can we make the type constraint in EPDataStore more specific?
One way is to create another protocol and require that an instance of (a type implementing)EPQuantity can be stored in our store if and only if the type returned by its getValue requirement conforms to this new protocol. Let's call this protocol EPPersistable:
protocol EPPersistable {}
Now, let's update EPDataStore with a more specific type constraint:
class EPDataStore<T: EPQuantity where T.EPQuantityValueType: EPPersistable> { func store(item: T) { } func getAll() -> [T]? { return nil } }
Now, if you try to create an instance of DataStore for EPSpeed:
let dataStore = EPDataStore<EPSpeed>()
the compliler will complain with Type 'Int' does not conform to protocol 'EPPersistable'. We can make the error go away by using an extenssion to add protocol conformance:
extension Int: EPPersistable {}
and if we want to persist instances of EPAcceleration, we would also need:
extension Double: EPPersistable {}
That's nice, but let's get back to our isolated components. Each is holding its own CoreData stack, a CoreData wrapper to translate between managed objects and domain objects, and a DataStore to handle data integrity or any other required logic. As a DataStore dependency, we want the CoreData wrapper to provide a mapping between objects conforming to the EPQuantity protocol and the corresponding managed objects that will land in our CoreData storage. As an example, EPSpeed would be matched by the following managed object:
@objc(EPSpeedCoreData) class EPSpeedCoreData: NSManagedObject { // Insert code here to add functionality to your managed object subclass } extension EPSpeedCoreData { @NSManaged var speed: Int32 }
Then, in order to be able to handle different managed objects through a common interface, we could add the following protocol:
protocol EPCoreDataQuantity { associatedtype EPCoreDataQuantityValueType func getValue() -> EPCoreDataQuantityValueType func setValue(value: EPCoreDataQuantityValueType) } extension EPSpeedCoreData:EPCoreDataQuantity { func getValue() -> Int { return Int(speed) } func setValue(value: Int) { speed = Int32(value) } }
And finally, the skeleton of our universal CoreData wrapper could be defined by the following set of requirements:
protocol EPPersistance { associatedtype EPQuantityType func write(item: EPQuantityType) func read() -> [EPQuantityType]? }
and fullfilled by the following type:
class EPCoreDataWrapper<T: EPCoreDataQuantity, U: EPQuantity where T.EPCoreDataQuantityValueType: EPPersistable, U.EPQuantityValueType: EPPersistable>: EPPersistance { func write(item: U) { } func read() -> [U]? { return nil } }
And obviously, our EPDataStore needs an instance of compatible CoreData wrapper, so obviously one could be tempted to try something like this:
class EPDataStore<T: EPQuantity where T.EPQuantityValueType: EPPersistable> { let persistance: EPPersistance init(persistance: EPPersistance) { self.persistance = persistance } func store(item: T) { persistance.write(item) } func getAll() -> [T]? { return persistance.read() } }
But we will get the familiar error - this time regarding EPPersistance: Protocol 'EPPersiatance' can only be used as a generic constraint because it has Self or associated type requirements.
The solution to this problem is commonly known as Type Erasure, a technique that I also describe shortly in my other post Around Type Erasure in Swift. We cannot refer to EPPersistance directly because it has an associated type. We need to hide this fact from the compiler by wrapping up the EPPersistance inside another type that will not exbit the limitation that EPPersistance has. To achieve that, we create a type that wraps an instance conforming to the EPPersistance protocol:
class EPAnyPersistance<T: EPQuantity where T.EPQuantityValueType: EPPersistable> : EPPersistance { let _write: T -> () let _read: () -> [T]? init<BaseType: EPPersistance where BaseType.EPQuantityType == T>(base: BaseType) { _write = base.write _read = base.read } func write(item: T) { _write(item) } func read() -> [T]? { return _read() } }
Now, it is safe to add a reference to the persistance object to our data store:
class EPDataStore<T: EPQuantity where T.EPQuantityValueType: EPPersistable> { let persistance: EPAnyPersistance<T> init(persistance: EPAnyPersistance<T>) { self.persistance = persistance } func store(item: T) { persistance.write(item) } func getAll() -> [T]? { return persistance.read() } }
We can create an instance of EPDataStore for EPSpeed type as follows:
let dataStoreForSpeed = EPDataStore<EPSpeed>(persistance: EPAnyPersistance(base: EPCoreDataWrapper<EPSpeedCoreData, EPSpeed>())) dataStoreForSpeed.store(EPSpeed(speed: 125)) dataStoreForSpeed.getAll()
Try it out in the playground: ExperimentingWithPersistanceInSwift.playground
Around Type Erasure in Swift
WWDC 2016 being already a past event (with lots of exiting material to watch), I've finally found more time to look more carefully to one of the most important programming paradims promoted in Swift: Protocol-Oriented Pragramming. Protocol-Oriented Programming in Swift was a great talk at WWDC 2015, where the concept was beautfully explained and demonstrated by Dave Abrahams. During this year's WWDC 2016 we have a follow-up: Protocol and Value Oriented Programming in UIKit Apps, which immediatelly invites us to watch again another great session from WWDC 2015: Building Better Apps with Value Types in Swift.
The protocol-oriented programming is a powerful concept and I still need to digest it and reflect on it trying to use it in our production code. Here we will take a short look at a technique called type erasure, which comes handy when dealing with generic protocols.
The problem
Say you have a protocol:
protocol Logger { associatedtype LoggerItemType func log(item: LoggerItemType) }
and you have a type that uses it:
struct Proccess { let logger: Logger }
Unfortunately, this will fail with compile error saying: Protocol 'Logger' can only be used as a generic constraint because it has Self or associated type requirements.
The solution
This problem can be solved by applying so called Type-Erasure technique in which we wrap the generic protocol, Logger in our case), in another type conforming to the same protocol (Logger again) and delegating all requirements to an internal object. Such wrappers are often named Any{Protocol}:
struct AnyLogger<LoggerItemType> : Logger { let _log: LoggerItemType -> () init<BaseType: Logger where BaseType.LoggerItemType == LoggerItemType>(base: BaseType) { _log = base.log } func log(item: LoggerItemType) { _log(item) } } struct IntLogger: Logger { func log(item: Int) { print("IntLogger: \(item)") } } struct Process<T> { let logger: AnyLogger<T> } let process = Process(logger: AnyLogger(base: IntLogger())) process.logger.log(24) // prints '24'
This solves the problem but when Logger protocol has many requirements, AnyLogger has to bridge all of them. Also every time you extend the Logger protocol, you need to update AnyLogger as well.
We can improve this situation - at the cost of an extra indirection - by introducing a logger provider and applying type-erasure on it:
protocol LoggerProvider { associatedtype LoggerType func get() -> LoggerType } struct AnyLoggerProvider<LoggerType>: LoggerProvider { let _get: () -> LoggerType init<BaseType: LoggerProvider where BaseType.LoggerType == LoggerType>(base: BaseType) { _get = base.get } func get() -> LoggerType { return _get() } } struct IntLoggerProvider: LoggerProvider { let intLogger: IntLogger func get() -> IntLogger { return intLogger } } struct Process<T> { let loggerProvider: AnyLoggerProvider<T> } let process = Process(loggerProvider: AnyLoggerProvider(base: IntLoggerProvider(intLogger: IntLogger()))) process.loggerProvider.get().log(24)
We see that, now we got one extra intermediate call, but we do not have to duplicate the Logger requirements in the wrapper.
Get the playground here: AroundTypeErasureInSwift.playground.
Further Reading
A Little Respect for AnySequence
StackOverflow. Anwer to question: How to use generic protocol as a variable type?
String conversions in Swift
Say you have your own custom class:
class MyClass { let counter: Int init(withCounter: Int) { self.counter = withCounter } }
Now, you want it to be convertible to String. One way of doing it is to create an extension to the String class itself like this:
extension String { init(_ myClass: MyClass) { self = "{ counter: \(myClass.counter) }" } }
This will let you do:
let str = String(MyClass(withCounter:25)) // str is "{ counter: 25 }"
This may seem to be sufficient in many cases, but it does not seem to be truly the Swift way. I realised this when working with testing framework (Nimble) and I realised that having just String extension does not make Nimble to produce correct string descriptions of my class instances when testing. In particular, and you can test it easily in the playground, this will not work as expected:
let str2 = "\(MyClass(withCounter:25))" // str2 is "MyClass"
For the string interpolation to work as expected in this case, you would rather have to do this:
let str3 = "\(String(MyClass(withCounter:25)))" // str3 is "{ counter: 25 }"
This is not how you want to use string interpolation though.
This blog describes how this is supposed to be done correctly in Swift 1 (still with lots of pain), and here is the updated version for Swift 2.
To summarise, this is how you can extend MyClass:
class MyClass: CustomStringConvertible { let counter: Int var description: String { return "{ counter: \(self.counter) }" } init(withCounter: Int) { self.counter = withCounter } }
Now, the string interpolation should work as expected. You can download the gist playground file here.
Using Swift Frameworks from Objective C target
The code in this post was tested with Xcode Version 7.2 (7C68).
The accompanying source code can be found at github.com/mczenko/UsingSwiftFrameworksFromObjectiveC
When working with legacy iOS products it may happens that you need to integrate with main application target that contains only Objective-C code. You want to take advantage of Swift but you want this nicely separated from the old legacy code. Creating a framework that will contain your Swift code is the answer.
There is no something like a Swift framework. A framework can include both Objective-C and Swift code. In this short post we focus on a framework containing only Swift code.
If you want to share your Swift code, a framework is necessary. You can't just use a static library as they do not support Swift code.
The following steps describe the whole process of creating a framework and adding it to an Objective-C-only target.
If you want to follow with the source code from github.com/mczenko/UsingSwiftFrameworksFromObjectiveC please clone the repository and checkout the very first commit with comment Initial commit. (commit 73e8dde) and then follow the steps. The last commit corresponds to the final state.
Step 1 - add a taget
Select your project in the Project Navigator and create a new target:
and name it ExampleSwiftFramework:
Step 2 - make the tests logical
The idea of Logical Unit Tests is less widely used in the Xcode community, but you can still make sure that your framework tests execute only against your framework code without depending on the application code (no application instance, no app delegate, no views, etc). Even though Apple does not seem to distinguish anymore between application unit tests and logic unit tests, the latter is what we want here: your tests working only with the Swift code in your framework. For this to work you need to make sure that the Host Application seeting for the ExampleSwiftFrameworkTests is set to None:
Logic unit tests cannot be run on the device. They may only run in the simulator.
Step 3 - add example framework class
Now inside our example framework, add an example swift class:
import Foundation class ExampleSwiftFrameworkClass { let message: String init(withHelloMessage message: String) { self.message = message } func sayHello() -> String { return self.message } }
The tests for this class can be found in the ExampleSwiftFrameworkTests.swift file in the ExampleSwiftFrameworkTests target.
Step 4 - referring to the Swift framework from the main application target
Now, let's try to reference the ExampleSwiftFrameworkClass from our main application target - which does not contain any Objective-C code.
We will be modifying the viewDidLoad method of the ViewController class from the main application target. You will quickly realise that Xcode code completion does not work for our class - in this case it right - our class is not visible yet in the main application target.
To make our class visible you need to import the module it comes from. Add the following line somewhere on the top of your ViewController.m file:
@import ExampleSwiftFramework;
You may realise that the Xcode's completion already recognized the module, but the our ExampleSwiftFrameworkClass remains invisible to Xcode. To fix this, we need to do two things:
Mark your Swift framework class with @objc annotation, and make it inheritting from NSObject (if you do not, compiler will tell you that you can't use @objc annotation otherwise).
Make sure your class, the constructor, and the method you want to access from another target are public. The default visibility is internal which means your class and all its methods are accessible to any other class from the same target. But our framework and our main application are from different targets - that's why we need to use public for access control.
After the modifications, our ExampleSwiftFrameworkClass should look like this:
@objc public class ExampleSwiftFrameworkClass : NSObject { let message: String public init(withHelloMessage message: String) { self.message = message } public func sayHello() -> String { return self.message } }
If you try to run the app or the application tests (not framework tests) you may be surprised seeing that the app crashes with the following log output:
dyld: Library not loaded: @rpath/libswiftCore.dylib Referenced from: /Users/usename/Library/Developer/Xcode/DerivedData/UsingSwiftFrameworksFromObjectiveC-befsyfovlmeizgeypzhxolfxbaco/Build/Products/Debug-iphonesimulator/ExampleSwiftFramework.framework/ExampleSwiftFramework Reason: image not found
Xcode is quite intelligent in fixing most of the stuff for us (we will discuss this still in more details below), but there are somethings we have to take care for ourselves.
Step 5 - Fixing the missing reference to libswiftCore.dylib
If you are building an app that does not use Swift but embeds content such as a framework that does, Xcode will not include these libraries in your app. As a result, your app will crash upon launching...
The text quoted above comes from the Technical Q&A QA1881 Embedding Content with Swift in Objective-C. As resultion it instructs to set Embedded Content Contains Swift Code build setting to YES for the target that does not include Swift code itself but depends on a framework that does use Swift. Additionally, it recommends setting the Embedded Content Contains Swift Code to NO for the dependent framework target so that we do not get multiple copies of Swift libraries in the app.
In our example therefore, the only target where we need to set Embedded Content Contains Swift Code to YES is the main application target UsingSwiftFrameworksFromObjectiveC:
Other settings
We mentioned already that Xcode takes care for pretty mych everything. In some cases, however, especially when you deal with a legacy project, it may turn out handy to know what else may require our attention. We summarize the important points below:
Main application target - UsingSwiftFrameworksFromObjectiveC. We have to make sure that target depends on the framework, the framework is included in the Link Binary With Libraries build phase, and that the Embed Frameworks build phase contains the framework with the Destination set to Frameworks:
The framework - ExampleSwiftFramework target. In our case we do not want it to depend on the main application target UsingSwiftFrameworksFromObjectiveC:
ExampleSwiftFrameworkTests target. It depends on ExampleSwiftFramework only and includes ExampleSwiftFramework in the Link Binary With Libraries build phase:
Additional references
Technical Q&A QA1881 v2 - Embedding Content with Swift in Objective-C
Swift and Objective-C in the Same Project
How to Create a Framework for iOS
Build your own Cocoa Touch Frameworks, in pure Swift
console.log
Say I have a class in Javascript:
var Playground = function() { this.helloString = 'Hello'; }; Playground.prototype.hello = function() { return this.helloString(); };
and its instance:
var playground = new Playground();
Now, is there a difference between:
console.log(playground.toString());
and
console.log(playground);
It turns out there is and realizing it can help you printing more useful debug information.
The playground.toString() will call the default implementation of toString function which is defined on the Object.prototype. This string conversion function does not print any useful data in most of the cases. Here we will get:
[object Object]
as the output. In contrast, the console.log(playground); will print:
{ helloString: 'Hello' }
This is much more informative isn't it?
The question is, why is it happening at all. If you check documentation of the Console module, in the description of the console.log function you will read that:
If formatting elements are not found in the first string then util.inspect is used on each argument.
Indeed, if we call
console.log(util.inspect(playground));
then we will get:
{ helloString: 'Hello' }
as the output. For more formating options you may want to check the documentation of the util module.
The difference between console.log(playground.toString()); and console.log(playground); will be even more visible if you use javascript console in a browser. Browsers often provide their own implementation of console.
Finally, consider how you pass arguments to console.log:
console.log('playground:', playground);
will print:
playground: { helloString: 'Hello' }
while
console.log('playground:' + playground);
will print:
playground:[object Object]
Nice!
Javascript - create an instance without new operator
From Chapter 4 of Beautiful Javascript:
A suggested exercise for the reader is to create instances of a class without using the new keyword.
Would it be something like this?
var MyClass = function(helloString) { var instance = Object.create(MyClass.prototype) console.log('this[MyClass]:'); console.log(this); instance.helloString = helloString; return instance }; MyClass.prototype.sayHello = function() { console.log(this.helloString); }; var myClass = MyClass('Hello from MyClass'); myClass.sayHello(); console.log('MyClass.prototype:'); console.log(MyClass.prototype) var MyClass2 = function(helloString) { console.log('this[MyClass2]:'); console.log(this); this.helloString = helloString; }; MyClass2.prototype.sayHello = function() { console.log(this.helloString); }; var myClass2 = new MyClass2('Hello from MyClass2'); myClass2.sayHello(); console.log('MyClass2.prototype:'); console.log(MyClass2.prototype);
Check it out here: http://jsfiddle.net/mczenko/axvcvv4n/
Sublime Power User?
Ok, let's see if I can save 30mins a day: Sublime Text Power User.
ES6 with Sublime Text 3
If you want to have syntax aware rendering in Sublime Text 3 for new Javascript (ES6), follow the following steps.
On addyosmani/es6-tools there is a lot of good stuff. Search for Sublime. You will find ES6 syntax highlighting for Sublime Text and TextMate.
Follow the instructions: install Package Control if you haven't yet, then use your Command Palette (CMD+Shift+P) and select JavaScript Next from the Package Control: Install Package dropdown list.
I did not test it with Sublime Text 2, but it seems that the syntax highlighting plugin is actually written for Sublime Text 2.
module.exports vs exports in Node.js
This post is a nice summary of the differences between module.exports and exports in Node.js:
Node.js Module – exports vs module.exports
The article comes from 2011 but then updated in 2014. You may want to take a look at the comments, they add valuable insights. I especially value the one from Adam Ahmed, and I decided to include it below for my own convenience.
module.exports vs exports
It might help clarify things to show how they are initialized. Essentially before your code runs, there is code that runs like this:
var module = {...}; // stuff var exports = module.exports = {};
So both variables initially point to the same empty Object, but of course can be reassigned to any other value instead. So if you reassign the exports variable, it doesn’t affect module.exports. Similarly, if you reassign module.exports, it no longer affects exports. They will be pointing at different values. What makes module.exports the “real deal” is that when someone require()s your module, require essential does this:
function require(moduleName) { var module = getModule(moduleName); return module.exports; }
It might also help to say that module is the real deal more than module.exports is. Take this code for example:
var truth = module; var notExports = module.exports; truth.exports = { genre: ‘Rock’ }; notExports.genre = ‘Blues';
‘Rock’ will be exported. Since truth is pointing to module, it can make lasting changes to the exports that notExports can’t.
Documentation for software developers
Even though the best documentation for your code are automated tests, you still will need to record other stuff as well.
Writing documentation must be seamless and efficient, otherwise you won't write it regullary. For this reason I never liked Wiki because I've always considerered Wiki markup to be too heavy for this purpose.
Since around 5 years I am using Confluence from Atlassian to host some of my documentation. Its markup is way lighter than ordinary Wiki and it is quite easy to include code snippets. But Confluence is based on a heavy environment (Java Tomcat), and even though its markup is quite light, it is not the lightest you can get today. My favorite since more than a year is Markdown.
The simplest way to create a comprehensive documentation system based on Markdown is to simply create a repository on Bitbucket (Bitbucket allows you to create private repositories) and then just create your documentation files using markdown (make sure that each file has the .md extension). Bitbucket (but also Stash and Github) can render markdown which means you can read your docs online without cloning and you get full git history for free.
There are other solutions based on Markdown. Just take a look at the following to get inspired.
http://beautifuldocs.com,
https://beegit.com/writing-software,
http://writeapp.net/mac/.
And well, if you would like to create the greatest markdown-based system supporting writing documentation for developers, let me know, maybe we could work on it together.
Why AppCode?
I am evaluating AppCode and based on my experience with IntelliJ I am quite sure that it will be a great improvement to my workflow. Here I would like to record a nice blog from Ryan Abel from Atomic Object: Why I Prefer AppCode over Xcode.
It is quite old but still pretty nice summary and some comments below add even more value.
AppCode - a nice tool, but incomplete UIDesigner
> Update. After my comment on the JetBrains page JetBrains updated their description of the UIDesigner. Now one can learn quickly that there is somthing missing: >
>
Original Post: I do not like unfair marketing. I am using IntelliJ and I consider it one of the better tools for developers. Unfortunately, JetBrains sometimes overlloks to include the full specs of a product they sell and all story starts looking like a typical marketing talk. A good example is AppCode. Please visit AppCode's what's new page. Try to read something about UIDesigner. Can you find quickly any information that would suggest that there is anything missing in it? I can't. It seems it is all perfect. It is not, it does not support Xcode 6 Universal Story board format, which simply speaking makes it useless for any current UI work. It does not make AppCode useless, it is still great, but it makes UIDesigner useless. The worst think about it is that in order to learn such a basic thing you have to contact JetBrains (they did responded promptly though even if I just sent a comment under their AppCode page).
I would like (but who am I to ask) that AppCode page has on the very front page a separate tab named "compatibility" where it states clearly - "AppCode UIDesigner does not work with the Xcode 6 universal story boards" (you can make it nice without loosing customers I am sure). This is so much more customer oriented than expecting people like me (and maybe I am the only less intelligent one) to scan through "small text" where they probably do state it clearly. This would save me some precious time.
I consider IntelliJ products being a good value for the price, so why to lower the reputation by trying to sell an ad...
BTW: JetBrains asks the full price for AppCode.
git reset
git reset is one of the commands I use less frequently, which means it is easier to forget when and how to use it to achieve the desired effect.
Let's say I have just commited something and then I decided that what I commited should not be commited. Basically, I want to get rid of the very last commit, no matter the consequences.
$ mkdir repo $ cd repo $ git init Initialized empty Git repository in /Users/mczenko/Projects/learning/test_git/repo/.git/ $ echo "file1" > file1.txt $ git add file1.txt $ git commit -am 'Initial commit.' [master (root-commit) c9859f4] Initial commit. 1 file changed, 1 insertion(+) create mode 100644 file1.txt $ git log --oneline c9859f4 Initial commit.
Now, I add another file and I commit it to the repository:
$ echo "file2" > file2.txt $ git add file2.txt $ git commit -am "Added file2.txt" [master 11d160b] Added file2.txt 1 file changed, 1 insertion(+) create mode 100644 file2.txt $ git log --oneline 11d160b Added file2.txt c9859f4 Initial commit.
This will be our invariant, from which we will investigate the impact of variuos forms of git reset.
git reset --hard
Let's say I decided that I actually do not need file2.txt:
$ git reset --hard c9859f4 HEAD is now at c9859f4 Initial commit. $ git log --oneline c9859f4 Initial commit. $ ls file1.txt
If after that you realized that there was somathing important in file2.txt and you want to get it back, there is a good news: git actually did not remove your file:
$ git reflog c9859f4 HEAD@{0}: reset: moving to c9859f4 11d160b HEAD@{1}: commit: Added file2.txt c9859f4 HEAD@{2}: commit (initial): Initial commit. $ git checkout -b recovered 11d160b Switched to a new branch 'recovered' $ ls file1.txt file2.txt $ cat file2.txt file2 $ git checkout master Switched to branch 'master' $ git merge recovered Updating c9859f4..11d160b Fast-forward file2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 file2.txt $ git branch -d recovered Deleted branch recovered (was 11d160b). $ git reflog 11d160b HEAD@{0}: merge recovered: Fast-forward c9859f4 HEAD@{1}: checkout: moving from recovered to master 11d160b HEAD@{2}: checkout: moving from master to recovered c9859f4 HEAD@{3}: reset: moving to c9859f4 11d160b HEAD@{4}: commit: Added file2.txt c9859f4 HEAD@{5}: commit (initial): Initial commit.
We see that git reflog records a lot of useful information about the repository, not only the commits.
What's intersting here is that the recovered commit preserves its original commit id:
Original:
$ git log --oneline 11d160b Added file2.txt c9859f4 Initial commit.
Recovered
$ git log --oneline 11d160b Added file2.txt c9859f4 Initial commit.
We are now back with the original content. Let's try another version of git reset: soft reset.
git reset --soft
git reset --soft leaves the content in the staged area. Handy if you simply want to revise last commit:
$ git reset --soft c9859f4 $ ls file1.txt file2.txt $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: file2.txt
I use git reset --soft when I want to move the last commit to a new branch:
$ git reset HEAD . $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) file2.txt
Now I can easily move the changes I've done to a new branch:
$ git checkout -b new_branch Switched to a new branch 'new_branch' $ git status On branch new_branch Untracked files: (use "git add <file>..." to include in what will be committed) file2.txt nothing added to commit but untracked files present (use "git add" to track)
We can now commit file2.txt and then merge it with master:
$ git add file2.txt $ git status On branch new_branch Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: file2.txt $ git commit -m "file2.txt added again." [new_branch 14a6c4b] file2.txt added again. 1 file changed, 1 insertion(+) create mode 100644 file2.txt $ git status On branch new_branch nothing to commit, working directory clean $ git log --oneline 14a6c4b file2.txt added again. c9859f4 Initial commit. $ git checkout master Switched to branch 'master' $ git merge new_branch Updating c9859f4..14a6c4b Fast-forward file2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 file2.txt $ git log --oneline 14a6c4b file2.txt added again. c9859f4 Initial commit.
Of course, in this case the new commit has new commit id.
git reset
Finally, what does simple git reset do?
$ git reset c9859f4 $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) file2.txt nothing added to commit but untracked files present (use "git add" to track)
As we see, ordinary git reset marks the changes from the last commit as untracked without going through the staging area.
Do you see a mistake in this post, or you think something should written in a less ambiguous way? Please let me know. It is not my intention to have anyone learing gits the wrong way because of my incompetence.
External references:
[1] reflog, your safety net
Saving your time with git --autosquash
In this post a simple but quite common scenario. You have just committed something to your current branch and then you realised that you forgot something. For this example let's assume you've just created a fresh git init and you have just made your Initial Commit. The thing you forgot is README.md file (just as an example). Now you want to fix the thing up: you want adding the README.md to belong to the Initial Commit.
$ git init $ echo "FileA" > FileA.txt $ git add --all . $ git commit -am 'Intial commit.' [master (root-commit) 1c1a88d] Intial commit. 1 file changed, 1 insertion(+) create mode 100644 FileA.txt $ git log --oneline 1c1a88d Intial commit. $ echo "## git --autosquash is great" > README.md $ git add README.md $ git commit --fixup HEAD [master deaf9ef] fixup! Intial commit. 1 file changed, 1 insertion(+) create mode 100644 README.md $ git log --oneline deaf9ef fixup! Intial commit. 1c1a88d Intial commit. $ git rebase -i --autosquash --root master
This will open the file looking like this:
pick 1c1a88d Intial commit. fixup deaf9ef fixup! Intial commit.
Now you just hit cmd+W (Sublime Text2) to save the file and you are done! This should be the output:
[detached HEAD cb87174] Intial commit. 2 files changed, 2 insertions(+) create mode 100644 FileA.txt create mode 100644 README.md Successfully rebased and updated refs/heads/master. $ git log --oneline cb87174 Intial commit.
(Note this is now a completely new commit - the history has been rewritten. This is why you have to pay special attention if you do this on a branch that you already share with other folks.)
I own you one extra explanation here: I am using --root master just because I am on the master branch and there is nothing before Initial commit. Normally I could use something like HEAD~2 to point to the commit before the commit I want to fix the next commit up to. Finally, it is worth noticing that there is a --squash equivalent of the --fixup commit option.
Android Studio Beta
Google just announced:
Android Studio is a new Android development environment based on IntelliJ IDEA. It provides new features and improvements over Eclipse ADT and will be the official Android IDE once it's ready.
Very Nice!
From Authenticating JSON APIs, through SNI, to pyenv
What would be a pragmatic approach to authenticate JSON APIs? I did some short googling just to see what the most popular options are. If you're are using Google App Engine endpoints, then you can take advantage of all good stuff from Google (I was even writing about Testing Google App Engine Endpoints in one of my previous entires). But what if you are on your own, let's say you're hosting your own Ruby on Rails app?
I really like Rails Cast Securing an API. It really gives a nice overview of the state of the art approaches one may take to give JSON APIs a bit more protection. The cast is from May 2012 - it would be nice to see an updated version (or there're is nothing really new on this topic?). In the end, though, OAuth comes as the most complete, mainstream solution and the cast will point you out to the right direction. A Ruby gem recommended to follow is intridea/oauth2.
What was stopping me recently from using OAuth on my own server though, was my lack of awareness (I would even call it ignorance) of Server Name Indication - an extension to the TLS protocol that effectively lets you use different SSL certificates for different virtual hosts. Without this extension, providing separate SSL certificates for each site required separate IP addresses. This is because the TLS handshake happens before the server sees any HTTP headers, which is where name based virtual hosting gets the information about the hostname so that it can serve you the requested site. Server Name Indication addresses this issue by sending the name of the virtual domain as part of the TLS negotiation. SNI is quite well supported, only if you use Python, the wikipedia link above says that you need to work with Python version 3.2 at least (for ssl, urllib, and httplib modules). If you are using Ubuntu with Apache, you may really like a tutorial from DigitalOcean by Etel Sverdlov: How To Set Up Multiple SSL Certificates on One IP with Apache on Ubuntu 12.04. I will be giving it a try on my multiple-site ruby-on-rails installation soon.
This last remark about SNI support on Python motivated me to take a look at one more issue I was somehow avoiding recently. I am using Python virtualenv through virtualenvwrapper and I was already wondering how can I make virtualenv wrapper to use a specific version of Python. As you can read on this StackOverflow question both virtualenv and mkvirtualenv commands have -p option where you can specify the python version to be used (I hope to give a more complete example of this soon). So the question reduces to how can I conveniently switch between different Python versions (I should add on Mac OS as I am still somehow in the mindset that Windows was never really meant to be used as development platform besides Visual Studio and embedded systems - I hope the latter to change in the foreseeable future)? I was searching for something similar to the famous Ruby Version Manager. And lucky me! There is something and it is called pyenv - Simple Python Version Management. Again, I hope to have a more detailed example on this blog soon.