2017-02-01 18 views
15

Dưới đây là một ví dụ về những gì tôi muốn làm:Làm thế nào để in ra tên phương pháp và số dòng trong nhanh chóng

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) 
     { 
      let nm = NetworkModel() 
      nm.sendlog("file name :AppDelegate , line number : 288", info: " Failed to register: \(error)") 
     } 

kịch bản hiện tại tôi làm điều đó giá trị cứng mã hoá line numberfile name. nhưng có thể chọn theo chương trình line numberfile name.

+0

http://stackoverflow.com/a/24049928/771231 – Desdenova

+0

@Nazmul Hasan, xin vui lòng sử dụng in này ("Tên Chức năng: \ (# chức năng) Số dòng: \ (# dòng) ") chỉ nơi bạn muốn in –

Trả lời

34
Literal  Type  Value 

#file   String The name of the file in which it appears. 
#line   Int  The line number on which it appears. 
#column  Int  The column number in which it begins. 
#function  String The name of the declaration in which it appears. 
#dsohandle  String The dso handle. 

Ví dụ

print("Function: \(#function), line: \(#line)") 

Với các giá trị mặc định trong các thông số bạn cũng có thể tạo một hàm

public func track(_ message: String, file: String = #file, function: String = #function, line: Int = #line) { 
    print("\(message) called from \(function) \(file):\(line)") 
    } 

mà có thể được sử dụng như thế này

track("enters app") 

Trong Swift 2.1

Literal  Type  Value 

__FILE__  String The name of the file in which it appears. 
__LINE__  Int  The line number on which it appears. 
__COLUMN__  Int  The column number in which it begins. 
__FUNCTION__ String The name of the declaration in which it appears. 

để biết thêm xem tài liệu Check Documentaion

1
static func DLog(message: String, file: String = #file, function: String = #function, line: Int = #line, column: Int = #column) { 
    print("\(file) : \(function) : \(line) : \(column) - \(message)") 
} 
3

Bạn có thể sử dụng #function, #file, #line

Đây là việc thực hiện các phương pháp đăng nhập nhanh chóng: https://github.com/InderKumarRathore/SwiftLog

Dưới đây là đoạn

public func debugLog(object: Any, functionName: String = #function, fileName: String = #file, lineNumber: Int = #line) { 
    #if DEBUG 
    let className = (fileName as NSString).lastPathComponent 
    print("<\(className)> \(functionName) [#\(lineNumber)]| \(object)\n") 
    #endif 
} 
Các vấn đề liên quan