How to print something to the console in Xcode?

Objective CXcode

Objective C Problem Overview


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

Thanks!

Objective C Solutions


Solution 1 - Objective C

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)

Solution 2 - Objective C

@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");

Solution 3 - Objective C

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.

Solution 4 - Objective C

@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.

Solution 5 - Objective C

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 enter image description here

I highly recommend seeing Debugging with Xcode from Apple


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

enter image description here

Solution 6 - Objective C

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.

Solution 7 - Objective C

In Swift with Xcode you can use either print() or NSLog().

print() just outputs your text. Nothing more.

NSLog() additionally to your text outputs time and other useful info to debug console.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3404166View Question on Stackoverflow
Solution 1 - Objective CLoganView Answer on Stackoverflow
Solution 2 - Objective CstktrcView Answer on Stackoverflow
Solution 3 - Objective CSpoofView Answer on Stackoverflow
Solution 4 - Objective CAkshit ZaveriView Answer on Stackoverflow
Solution 5 - Objective CmfaaniView Answer on Stackoverflow
Solution 6 - Objective CHoldOffHungerView Answer on Stackoverflow
Solution 7 - Objective CIgor MelashchenkoView Answer on Stackoverflow