How to print something to the console in Xcode?

后端 未结 6 1053
逝去的感伤
逝去的感伤 2021-02-06 20:45

How do you print something to the console of Xcode, and is it possible to view the Xcode console from the app itself?

Thanks!

相关标签:
6条回答
  • 2021-02-06 20:59

    You can also use breakpoints. Assuming the value you want is defined within the scope of your breakpoint you have 3 options:

    print it in console doing:

    po some_paramter
    

    Bare in mind in objective-c for properties you can't use self.

    po _someProperty
    po self.someProperty // would not work
    

    po stands for print object.


    Or can just use Xcode 'Variable Views' . See the image

    I highly recommend seeing Debugging with Xcode from Apple


    Or just hover over within your code. Like the image below.

    0 讨论(0)
  • 2021-02-06 21:00

    How to print:

    NSLog(@"Something To Print");
    

    Or

    NSString * someString = @"Something To Print";
    NSLog(@"%@", someString);
    

    For other types of variables, use:

    NSLog(@"%@", someObject);
    NSLog(@"%i", someInt);
    NSLog(@"%f", someFloat);
    /// etc...
    

    Can you show it in phone?

    Not by default, but you could set up a display to show you.

    Update for Swift

    print("Print this string")
    print("Print this \(variable)")
    print("Print this ", variable)
    print(variable)
    
    0 讨论(0)
  • 2021-02-06 21:01

    @Logan has put this perfectly. Potentially something worth pointing out also is that you can use

    printf(whatever you want to print);
    

    For example if you were printing a string:

    printf("hello");
    
    0 讨论(0)
  • 2021-02-06 21:09

    In some environments, NSLog() will be unresponsive. But there are other ways to get output...

    NSString* url = @"someurlstring";
    printf("%s", [url UTF8String]);
    

    By using printf with the appropriate parameters, we can display things this way. This is the only way I have found to work on online Objective-C sandbox environments.

    0 讨论(0)
  • 2021-02-06 21:13

    3 ways to do this:

    In C Language (Command Line Tool) Works with Objective C, too:

    printf("Hello World");
    

    In Objective C:

    NSLog(@"Hello, World!");
    

    In Objective C with variables:

    NSString * myString = @"Hello World";
    NSLog(@"%@", myString);
    

    In the code with variables, the variable created with class, NSString was outputted be NSLog. The %@ represents text as a variable.

    0 讨论(0)
  • 2021-02-06 21:21

    @Logan said it perfectly. but i would like to add an alternative here,

    if you want to view logs from just your application then you can make a custom method that keeps saving the log to a file in documents directory & then you can view that log file from your application.

    There is one good advantage for developers of the app after the app has been released & users are downloading it. Because your app will be able to send logs & crash reports to the developers (of course with the permissions of the device user !!!) & it'll be the way to improve your application.

    Let me know (To other SO users), if there is another way of doing the same thing. (Like default Apple feature or something)

    Let me know if it helps or you want some more idea.

    0 讨论(0)
提交回复
热议问题