如何在 Objective-C 的 NSLog 函数中输出 NSString 类型?

发布于 2024-12-18 01:44:04 字数 114 浏览 0 评论 0原文

我想要用户的姓名输入,并使用 NSString 在 NSLog 中输出该输入姓名。 我不知道哪个 % 符号以及如何输出它。 我可以使用 scanf() 函数吗? 请帮助我,我只是 Objective-C 的初学者。

I want a input from user their name and output that input name in NSLog using NSString.
I don't know which % sign and how to output that.
Can i use scanf() function for that?
Please help me , i am just beginner of Objective-C.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

洒一地阳光 2024-12-25 01:44:04

您可以对包括 NSString 在内的所有对象使用 %@。这将依次调用对象的 description 方法并打印适当的字符串。大多数对象已经有一个相当有用的表示(例如 NSArray 对象返回其所有内容的描述)。

You can use %@ for all objects including NSString. This will in turn call the objects description method and print the appropriate string. Most objects have a rather useful representation already there (e.g. NSArray objects return the descriptions of all their contents).

心不设防 2024-12-25 01:44:04

Mark Dylan 是将存储在 Name 变量中的名字。

NSString* Name = @"Mark Dylan";

此代码将允许您询问他们的名字并将其扫描到内存中,该内存将存储在 Name 变量中。

NSLog(@"What is your name?");
scanf("%@", &Name);

如果你想打印出你可以使用的变量;

NSLog(@"Your name is %@", Name);

Mark Dylan is the name which would be stored in the Name variable.

NSString* Name = @"Mark Dylan";

This code will allow you to ask their name and scan it into memory which will be stored in the Name variable.

NSLog(@"What is your name?");
scanf("%@", &Name);

If you want to print out the variable you can use;

NSLog(@"Your name is %@", Name);
紧拥背影 2024-12-25 01:44:04

%@ 就是你想要的。它适合像 NSString[YourViewController class] 这样的对象

%@ is what you want. It fit for object like NSString, [YourViewController class]

疯了 2024-12-25 01:44:04

要获取用户的输入,请使用 UITextFieldNSTextField。要将字符串输出到日志文件,您可以使用 NSLog,即:

NSString* userName = @"Zawmin";

NSLog(@"name = %@", userName);

To get input from the user use a UITextField or a NSTextField. To output a string to the log file you can use NSLog, ie:

NSString* userName = @"Zawmin";

NSLog(@"name = %@", userName);
你是年少的欢喜 2024-12-25 01:44:04

NSLog 接受格式字符串,因此您可以执行如下操作:

#include <stdio.h>
#include <Foundation/Foundation.h>

// 1024 characters should be enough for a name.
// If you want something more flexible, you can use GNU readline:
// <http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html>
#define MAX_NAME_LENGTH 1024

// Get name from user input
char name[MAX_NAME_LENGTH];
name[0] = '\0'; // just in case fgets fails
fgets(name, MAX_NAME_LENGTH, stdin);

// Put name into NSString object and output it.
NSString *name = [NSString stringWithUTF8String:name];
NSLog(@"%@", name);

%@ 适用于所有 Objective-C 对象。
如果要输出 C 字符串(char*const char*),请使用 %s。切勿将非文字字符串作为 NSLog 的第一个参数,因为这会打开安全漏洞。

NSLog accepts a format string, so you can do something like this:

#include <stdio.h>
#include <Foundation/Foundation.h>

// 1024 characters should be enough for a name.
// If you want something more flexible, you can use GNU readline:
// <http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html>
#define MAX_NAME_LENGTH 1024

// Get name from user input
char name[MAX_NAME_LENGTH];
name[0] = '\0'; // just in case fgets fails
fgets(name, MAX_NAME_LENGTH, stdin);

// Put name into NSString object and output it.
NSString *name = [NSString stringWithUTF8String:name];
NSLog(@"%@", name);

%@ works for all Objective-C objects.
If you want to output a C-string (char* or const char*), use %s. Never put a non-literal string as the first argument to NSLog as this opens security holes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文