Macros in Swift?

Does Swift currently support macros or are you planning to add support? I am currently scattering:

Log.trace(nil, function: __FUNCTION__, file: __FILE__, line: __LINE__) 

In different places of my code.

+46
macros swift
Jun 09 '14 at 5:40
source share
7 answers

In this case, you must add a default value for the macro parameters.

Swift 2.2 and up

 func log(message: String, function: String = #function, file: String = #file, line: Int = #line) { print("Message \"\(message)\" (File: \(file), Function: \(function), Line: \(line))") } log("Some message") 

Swift 2.1 and below

 func log(message: String, function: String = __FUNCTION__, file: String = __FILE__, line: Int = __LINE__) { print("Message \"\(message)\" (File: \(file.lastPathComponent), Function: \(function), Line: \(line))") } log("Some message") 

This is what the fatalError and assert functions do.

There are no macros other than conditional compilation already mentioned in another answer.

+52
Jun 09 '14 at 8:43
source share

Apple docs claim that:

Declare simple macros as global constants and translate complex macros into functions.

You can still use # if / # else / # endif, but I feel that they will not introduce macro functions, the language is simply not needed.

+14
Jun 09 '14 at 5:46
source share

Since Xcode 7.3, the compile time constants __FILE__ __FUNCTION__ and __LINE__ have become more attractive #file #function and #line respectively.

+5
Mar 27 '16 at 16:01
source share

lastPathComponent needs NSURL , so I changed the code above:

 func log(message: String, function: String = __FUNCTION__, file: String = __FILE__, line: Int = __LINE__) { let url = NSURL(fileURLWithPath: file) print("Message \"\(message)\" (File: \(url.lastPathComponent ?? "?"), Function: \(function), Line: \(line))") } log("some message") 
+3
Nov 20 '15 at 9:38
source share

Here is the updated Swift 2 answer.

 func LogW(msg:String, function: String = #function, file: String = #file, line: Int = #line){ print("[WARNING]\(makeTag(function, file: file, line: line)) : \(msg)") } private func makeTag(function: String, file: String, line: Int) -> String{ let url = NSURL(fileURLWithPath: file) let className = url.lastPathComponent ?? file return "\(className) \(function)[\(line)]" } 

Usage example:

 LogW("Socket connection error: \(error)") 
+2
Jan 20 '16 at 3:50
source share

Macros are evil, but sometimes you just need them. For example, I have

 struct RegionEntity { var id: Int! } 

And I want to put instances of this structure in Set. Therefore, I have to comply with this Hashable protocol.

 extension RegionEntity: Hashable { public var hashValue: Int { return id } } public func ==(first: RegionEntity, second: RegionEntity) -> Bool { return first.id == second.id } 

Great. But what if I have dozens of such structures and the logic is the same? Maybe I can declare some protocol and disagree with Hashable. Let check:

 protocol Indexable { var id: Int! { get } } extension Indexable { var hashValue: Int { return id } } func ==(first: Indexable, second: Indexable) -> Bool { return first.id == second.id } 

Well, that works. And now I'm going to bind my structure to both protocols:

 struct RegionEntity: Indexable, Hashable { var id: Int! } 

Nope. I cannot do this because Equatable requires a == operator with Self, and for RegionEntity there is no == operator. Swift makes me copy the confirmation code for each structure and just change the name. With a macro, I could do this with only one line.

+1
Feb 15 '17 at 22:13
source share

There is a way to use macros in swift (but this is used in Mixed of c and swift)

declare your macros in Project-name-Bridging-Header.h

 #define YOUR_MACRO @"Description" 

or create a separate macro file for "macros.h"

import this header "macros.h" into the Bridging-Header.h file.

now just save your project, your macros will go to the fast file.

if you don’t want the c object code in your quick project ... just create a dummy cocoa sensory classes, it will create a bridge title and then use my method ...

-one
Oct 14 '16 at 11:32
source share



All Articles