Implement the MD5 algorithm using Swift & introduce a third-party class library (MBProgressHUD), and implement the MD5 algorithm using Swift
Previously, the project used the MD5 encryption algorithm written by objc, and recently used swift to rewrite previous projects. Fortunately, it was finally solved. This solution also introduces third-party class libraries, such as MBProgressHUD (SVProgressHUD) and some other good controls.
The solution is to use the objc and swift mixed programming methods.Bridging-headerFile.
What is Briding-header? You can simply understand that in a project developed using the swift language, introducing the objective-c file is a series file that needs to be made. It is like setting up a bridge, so that swift can also call the objective-c Class Library and frame.
So the question is, how can we create this Briding-header file?
1.Make sure that your project is built using swift
Select language in the figureSwift
2.Create a new class and select the languageObjective-c,
This step is only used to generate a file we need. Then we will delete the classes created in this step, so the class name can be set up at will.
Any write class name. after next, we will get a prompt indicating whether to create this bridging-header file. If we select Yes, we can see such a file in the directory.Xxx-bridging-header.hFile,
Then remove all the content in this file and remove the objc. H and. mTwo files (you can save them if they are useful ).
3.NextXxx-bridging-header.hImport to the various files we want.
Two instances are imported in my instance project. The first line is to write the MD5 algorithm, and the second line is to call a third-party class library, MBProgressHUD
Let's talk about it separately:
MD5 Algorithm
We needImport <CommonCrypto/CommonDigest. h>
After thatAny. swiftFile, write the following code
extension String{ func md5() ->String!{ let str = self.cStringUsingEncoding(NSUTF8StringEncoding) let strLen = CUnsignedInt(self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)) let digestLen = Int(CC_MD5_DIGEST_LENGTH) let result = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLen) CC_MD5(str!, strLen, result) var hash = NSMutableString() for i in 0 ..< digestLen { hash.appendFormat("%02x", result[i]) } result.destroy() return String(format: hash as String) }}
My Xcode version is beta5 of version 6.3, because 6.1.1 always jumps out of an error of XXXkit crash, which is particularly annoying. It is useless to find a method on the Internet for a long time, I had to go to the apple official website to install the beat version 6.3, which is much better.
Updated in 6.3Apple watch(Careful friends can find the shadows of apple watch in the above figure) andSwift1.2So some swift syntaxes may be slightly different, but it should have little impact. Here is my reference to stackoverflow's question:
Another great god is probably using the same version as me, but there are some minor differences in the string (format) statement.
In this way, we can directly write it to call the md5 Algorithm:
var str = "test123"var str_Md5 = str.md5()
It's easy, because I joined
Extension StringThen, the String class has a function md5, and the result is to directly return the MD5 encrypted result of the current str
MBProgressHUD
This is simpler. Go to GitHub to download the latest MBProgressHUD and. h and. copy the m file to the project, you can see in the previous picture, and then add the second line in the bridging-header.h
# Import "MBProgressHUD. h"
Then you can generate the following in your project:
// Show the loadingNotification let loadingNotification = MBProgressHUD. showHUDAddedTo (self. view, animated: true) loadingNotification. mode = MBProgressHUDModeIndeterminate loadingNotification. labelText = "querying ..."
Then eliminate
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
I will not elaborate on the usage of MBProgressHUD. It is a powerful and beautiful third-party control. You can study it on your own.