Why my 0.3 MB image occupies around 10 MB on RAM?
5 (2)

Click to rate this post!
[Total: 2 Average: 5]

You are writing an application that has a long list of entries, with each entry containing an image, the total download size of all images is about 10 MB, but the images take around 200 ~ 300 MB on RAM, you wonder why 🧐?

RAM normally does not understand images that are compressed, they are stored as raw bitmaps, even if the image is compressed, it gets inflated into memory as a raw image.

Image Size on RAM = (pixels height × pixels width × color depth bytes)

The following image takes around 300 KB on disk and has an sRGB color profile, which is 24 bits (8 bits per channel).

Unsplash (CC0)

The size of the image on the RAM would be:

Image Size on RAM = (1665  ×  2081 × 3) bytes = 9.8MB

In Swift, Kingfisher comes with an option to downsize images according to the screen scale, so you can have images in a reasonable size even if they come largely from the server.

import UIKit
import Kingfisher

extension UIImageView {
    
    func setImageAsThumb(url:String) {
        let formattedURL = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
        let scale = UIScreen.main.scale
        let resizingProcessor = ResizingImageProcessor(referenceSize: CGSize(width: 50.0 * scale, height: 50.0 * scale))
        self.kf.setImage(with: URL(string: formattedURL), placeholder: nil, options: [.processor(resizingProcessor)])
    }

}

Swift Lexical Structure
5 (2)

Click to rate this post!
[Total: 2 Average: 5]

Swift lexical structure, consists of valid tokens (lowest-level building blocks) that form the structure of any swift program, these tokens describe the rest of whole swift language…

A token consists of an identifier, keyword, punctuation, literal, or operator.

1) Identifiers:
An example of an identifier is a variable name, for example here “pet” is an identifier.

let pet = "Happy Dinosaur 🦖";

Identifiers support unicode characters, you can name you variable in you native language, and as in other programming languages, you cannot use keywords as identifiers, this is still possible if you surrounding a keyword with back-ticks,

var `var` = "var"

examples of unicode identifiers are

var _latitude = 32.0;
var アップル = "apple"
;

2) Keywords:
The list of basic keywords in swift are listed below, see (Swift Reserved Keywords) for comprehensive list and details.

classdeinitenum
extensionfuncimport
initletprotocol
staticstructsubscript
typealiasvarbreak
continuedefaultdo
elsefallthroughif
inforreturn
switchwherewhile
asdynamicTypeis
newsuperself
SelfType__COLUMN__
__FILE____FUNCTION____LINE__
associativitydidSetget
infixinoutleft
mutatingnonenonmutating
overrideprecedenceprefix
rightsetunowned
unowned(safe)unowned(unsafe)weak
willSet

3) Literals:
literals fall into 3 categories, integer, floating point, and string literals

Integer Literals
var a = 10

//Binary
var b = 00010100b


//Hexadecimal
var c = 14x


//Octal
var d = 24o


leading zeros will be ignored by the compiler, and the use of underscores is possible to increase readability.

var a = 100_000_000

Floating Point Literals
//Simple floating point number
var a = 10.7


//Exponent floating point number
var b = 10.6e2

var c = 10.1e-2
//Exponent floating point number

//Hexa decimal exponent
var d = 0xAp2


//Hexa decimal exponent
var d = 0xAp-2


String Literals

String literals are characters are enclosed within double quotes. Strings can contain escape sequences to represent characters like qoutes. Example for string literal is shown below.

var a = “test”
var a = “Hello\nWorld”

\0 Null Character
\ Backslash
\t Horizontal Tab
\n New line
\r Carriage Return
\” Double Quote
\’ Single Quote


4) Operators:
There are different operators supported in swift which includes
+ : Addition
– : Subtraction
* : Multiplication
/ : Division
% : Remainder
^ : Exponent
& : Bitwise And
&& : Logical And
| : Bitwise Or
|| : Logical Or
++ : Increment Operator
– : Minus
~ : Bitwise Not
< : Less Than
> : Greater Than
… etc.

Keep in mind, as in Swift’s official documentation, this is a list of reserved punctuation and can’t be used as custom operators:
(){}[].,:;=@#& (as a prefix operator), ->`?, and ! (as a postfix operator)”

Swift Whitespace:
White spaces are used to separate tokens and to distinguish prefixes, otherwise it’s normally omitted by the compiler.

Swift Comments:
these are statements that are ignored by the compiler, and meant for documentation purposes of our code, they could be either one-line or multi-line.

// This is a single line comment

/* Multi line (block) comment - can have
more than one line! */



What is Swift Programming Language?
5 (1)

Click to rate this post!
[Total: 1 Average: 5]

Swift is apple’s new open source programming language, that was introduced at Apple’s 2014 Worldwide Developers Conference (WWDC), it’s used to develop for iOS, macOS, watchOS and tvOS, mainly to work with Apple’s Cocoa and Cocoa Touch frameworks, it quickly became one of the fastest growing languages nowadays, hence open source under the Apache 2.0 license, other uses appeared including writing backend server side code using vapor framework.

Swift Programming Language

The main focus with swift is to be concise, more expressive, fast and less prone to error (safer) than Objective-C, with modern features language.

The man behind swift is Chris Lattner, who worked at Apple Inc. as Director of the Developer Tools department, leading the XCode, Instruments, and compiler teams, and the main author of LLVM (low level virtual machine), and CLang (replaces the full GNU Compiler Collection and intended to work atop LLVM).

To make onboarding easier for new comers, swift can run in playground, or a web-based REPL like this.
REPL stands for “Read Eval Print Loop”, it’s a command-line environment with experience similar to interpreted languages.

Chris Lattner (twitter @clattner_llvm)

Swift is a type-safe general-purpose & multi-paradigm language, the design goal of such languages is to allow programmers to use the most suitable programming style and associated language constructs for a given job, considering that no single paradigm solves all problems in the easiest or most efficient way, swift provides its own version of C/Obj-C types, along with powerful versions of the three primary collection types, Array, Set, and Dictionary, one other type it offers is tuple, so you can pass groups of vales, which is not available in Obj-C.

Language paradigms are a lot like musical genres, they’re messy things and we can argue about where to draw the lines, Sometimes swift is object-oriented, other times is functional. And it shines when generic.

The main Swift programming language consists of different projects, they are mentioned as below in swift.org site.

1- The Swift compiler command line tool
2- The standard library bundled as part of the language
3- Core libraries that provide higher-level functionality
4- The LLDB debugger which includes the Swift REPL
5- The Swift package manager for distributing and building Swift source code
6- Xcode playground support to enable playgrounds in Xcode.