Implementing MD5 algorithms with Swift & Introducing third-party class libraries (MBPROGRESSHUD)

Source: Internet
Author: User
Tags md5 encryption

The previous project was a MD5 encryption algorithm written in OBJC, which was recently rewritten with Swift to rewrite the previous project and encountered this problem. Fortunately, finally solved. By the way, how else can we introduce third-party class libraries, such as Mbprogresshud (Svprogresshud), and some particularly good controls

The solution is actually to use the OBJC and swift hybrid programming method, using the bridging-header file.

What is Briding-header? You can simply understand that in a project developed in Swift language, the introduction of the Objective-c file is a concatenation file that needs to be done, such as erecting a bridge, allowing Swift to invoke Objective-c's class library and frame, and so on.

So here's the question, how can I create this briding-header file?

1. Make sure your project is built with Swift

Language in the figure select Swift


2. arbitrarily create a new class, language selection objective-c,


This step is just to generate a file that we need, and then we'll delete The class that was built in this step, so the class name or whatever.


Write the class name arbitrarily, next, we will get a hint, whether we want to build this Bridging-header file, we choose Yes , we can see the directory more out of such a Xxx-bridging-header.h file,

Then remove everything from this file and remove the . h and. M two files of the OBJC generated in our previous step (if useful, of course, you can keep them down).

3. Next, import into the various files we want in xxx-bridging-header.h .

In my instance project import two, the first line is to write the MD5 algorithm, the second line is to call the third-party class library, Mbprogresshud

Let's say it separately:


MD5 Algorithm

We need import <CommonCrypto/CommonDigest.h>

After that, in any of your . Swift files, 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 6.3 Beta5, because 6.1.1 will always jump out of a what xxxkit crash out of the error, particularly annoying, online for a long time to say that the method is no use, had to go to the Apple official website under a beat version of the 6.3 installation, feel much better.

6.3 has updated Apple Watch(a careful friend can find the shadow of Apple Watch in the image above) and swift1.2, so some swift syntax may be slightly different, but it should not affect much, This I refer to the answer of the great God on StackOverflow:


Another great God is estimated to be using the same version as me, but there are some small differences in the string (format) statement




In this way, we can write directly to invoke the MD5 algorithm:

var str = "Test123" var str_md5 = Str.md5 ()

very simple, because after adding extension string , the string class has a function MD5, the result is directly return the current STR MD5 encrypted results




Mbprogresshud

This is even easier, go to GitHub to download the latest Mbprogresshud, and then copy the. h and. m files into the project, which you can see in the previous image, and then add the second line in the Bridging-header.h

#import "MBProgressHUD.h"

Then you can generate this in your project:

   Show the loadingnotification let            loadingnotification = Mbprogresshud.showhudaddedto (Self.view, Animated:true)            Loadingnotification.mode = mbprogresshudmodeindeterminate            loadingnotification.labeltext = "Query ..."

and then this eliminates

Mbprogresshud.hideallhudsforview (Self.view, Animated:true)

on the usage of Mbprogresshud I will not go into detail, a very powerful and beautiful third-party control, you can study the next




Implementing MD5 algorithms with Swift & Introducing third-party class libraries (MBPROGRESSHUD)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.