/ swift

SFErrors and SFCore Swift μFrameworks

Recently I made two μFrameworks for Swift 1.2, the SFCore and SFErrors frameworks. The first one rely on the the second one. I will explain what they offer, even if the perimeter is narrow. I could be helpful anyway.

Also, they are Carthage compatible, which could make their integration quite easy for you.

They are available here on GitHub:

What do those μFrameworks offer?

SFErrors

SFErrors is totally inspired and based on the work of David Owens II https://github.com/owensd/SwiftLib/. It is composed of two main classes that are exposed:

  • SFError : which encapsulates en NSError object.
  • SFFailable : which will describe if an error occurred or not.

For example, instead of creating a function that accepts an inout NSError parameter, you could create a function that returns a SFFailable object or a couple of error status and object result. Let me show you.

func myFunctionThatCanReturnAnError(age: UInt) -> (SFFailable, AnyObject?) {
	if age > 18 {
		return (SFFailable.Success, "You are an adult")
		
	} else {
		return (
			SFFailable.Failure(
				SFError(
					code: 0, 
					domain: "adult.domain", 
					userInfo: ["Message":"this is the error description"]
				)
			), 
			"You are not an adult, sorry for that"
		)
	}
}

Then, you would check the result of calling this function that way, anywhere else in your code:

let result = myFunctionThatCanReturnAnError(17)
if result.0.failed {
	let error = result.0.error
	// ... etc.
}

This almost is what the SFErrors framework will do. Simple, but efficient in many cases.

SFCore

The SFCore framework offers a few helpers mainly on :

  • Structures
  • Files
  • Extensions
Structures

It contains a queue and a set structure. I should remove the set structure because Swift 1.2 introduced a native Set.

Files

It contains a file manager so that getting paths to the most common iOS directories is made easier.

Here are the different functions available in the SFFileManager class:

  • static func mainBundleDirectory() -> String?
  • static func cachesDirectory() -> String
  • static func documentsDirectory() -> String
  • static func temporaryDirectory() -> String
  • static func userFile() -> String
  • static func createFileAtPath(fileData: NSData, atPath path: String) -> SFFailable
  • static func removeFileAtPath(path: String) -> SFFailable
  • static func contentOfFileAtPath(path: String) -> String
  • static func createDirectoryInDocumentsDirectory(directoryName: String)
Extensions

The SFCore framework extends some of the native Swift objects because, frequently, when you begin a new project or work with different ones, you like to use the same functions that make you gain time.

I let you discover those ones, but here are the extended objects :

  • UIViewController
  • String
  • NSDate
  • Int
  • UInt
  • UIColor

Conclusion

As you can see, this is not the ultimate framework, I'm not sure I can yet call it a framework, by the way. But every time I get something I'm supposed to reuse, I'll add it and make this piece of sh.., I mean code, be greater, by the time.