눕개발자
134
2021-06-16 09:01:16
0
68

[swift] 이 print log를 어떻게 라벨에 출력할까요?



func cpuUsage() -> Double {
        var kr: kern_return_t
        var task_info_count: mach_msg_type_number_t

        task_info_count = mach_msg_type_number_t(TASK_INFO_MAX)
        var tinfo = [integer_t](repeating: 0, count: Int(task_info_count))

        kr = task_info(mach_task_self_, task_flavor_t(TASK_BASIC_INFO), &tinfo, &task_info_count)
        if kr != KERN_SUCCESS {
            return -1
        }

        var thread_list: thread_act_array_t?
        var thread_count: mach_msg_type_number_t = 0
        defer {
            if let thread_list = thread_list {
                vm_deallocate(mach_task_self_, vm_address_t(UnsafePointer(thread_list).pointee), vm_size_t(thread_count))
            }
        }

        kr = task_threads(mach_task_self_, &thread_list, &thread_count)

        if (kr != KERN_SUCCESS) {
            return -1
        }

        var tot_cpu: Double = 0

        if let thread_list = thread_list {

            for j in 0 ..< Int(thread_count) {
                var thread_info_count = mach_msg_type_number_t(THREAD_INFO_MAX)
                var thinfo = [integer_t](repeating: 0, count: Int(thread_info_count))
                kr = thread_info(thread_list[j], thread_flavor_t(THREAD_BASIC_INFO),
                                 &thinfo, &thread_info_count)
                if kr != KERN_SUCCESS {
                    return -1
                }

                let threadBasicInfo = convertThreadInfoToThreadBasicInfo(thinfo)

                if threadBasicInfo.flags != TH_FLAGS_IDLE {
                    tot_cpu += (Double(threadBasicInfo.cpu_usage) / Double(TH_USAGE_SCALE)) * 100.0
                }
            } // for each thread
        }
        
        let str = String(format: "%.2f", tot_cpu)
        print("+=====[Swift]CPU Used: ", str)
        
        return tot_cpu
    }

안녕하세요 개발중 궁금한점이 있어 질문드립니다!

해당 코드를 어떻게 라벨에 출력할까요?


출력하고 싶은 라벨입니다. 이 라벨은 OneController에 있고,

위 cpu log 코드는 TwoController에 있습니다. 

    @IBOutlet weak var cpu_use: UILabel!


    private var m_TwoController = TwoController()

로 클래스를 호출하였습니다.



0
  • 답변 0

  • 로그인을 하시면 답변을 등록할 수 있습니다.