GVKun编程网logo

cocoa – 使用NSString中断NSString,获取用于中断/分离的字符串之后的所有内容

25

对于想了解cocoa–使用NSString中断NSString,获取用于中断/分离的字符串之后的所有内容的读者,本文将是一篇不可错过的文章,并且为您提供关于c–为什么添加两个字符串文字不使用opera

对于想了解cocoa – 使用NSString中断NSString,获取用于中断/分离的字符串之后的所有内容的读者,本文将是一篇不可错过的文章,并且为您提供关于c – 为什么添加两个字符串文字不使用operator(const string&,const string&)?、c – 使用长度AND编码将const char *字符串转换为NSString、cocoa – NSString初始化、cocoa – 从NSAttributedString中的nsfilewrapper / nstextattachment获取文件名和路径的有价值信息。

本文目录一览:

cocoa – 使用NSString中断NSString,获取用于中断/分离的字符串之后的所有内容

cocoa – 使用NSString中断NSString,获取用于中断/分离的字符串之后的所有内容

我正试图从下面的Nsstring中获取DOE,JOHN:

IDCHK9898960101DL00300171DL1ZADOE,JOHN

我试图将字符串拆分为1ZA,因为这将是不变的.

这是我到目前为止所尝试过的,但它给了我与我正在寻找的相反的东西:

Nsstring *getTheNameOuttaHere = @"IDCHK9898960101DL00300171DL1ZADOE,JOHN";

 // scan for "1ZA"
 Nsstring *separatorString = @"1ZA";

 NSScanner *aScanner = [NSScanner scannerWithString:getTheNameOuttaHere];

 Nsstring *thingsScanned;

 [aScanner scanUpToString:separatorString intoString:&thingsScanned];

 NSLog(@"container: %@",thingsScanned);

输出:

container: IDCHK9898960101DL00300171DL

任何帮助都会很棒!谢谢!

解决方法

短:

[[getTheNameOuttaHere componentsSeparatedByString:@"1ZA"] lastObject];

c – 为什么添加两个字符串文字不使用operator(const string&,const string&)?

c – 为什么添加两个字符串文字不使用operator(const string&,const string&)?

编辑:我把这个职位重新格式化了.

为什么这样做:

struct A {};

struct B {
    B(A){}
};

void operator+(const B&,const B&) {}

int main()
{
    A a1,a2;
    a1 + a2;
}

这不是吗?

struct B {
    B(const char*){}
};

void operator+(const B&,const B&) {} //error: invalid operands of types 'const char [6]' and 'const char [6]' to binary 'operator+'|

int main()
{
    "Hello" + "world";
}

本质上,在第一个例子中,a1和a2都通过隐式转换转换为B对象,并使用运算符(const B& const B&)来添加.

从这个例子中,我将期望“Hello”和“world”通过隐式构造函数再次转换为B对象,并使用operator(const B& const B&)来相互添加.相反,有一个错误,这表示C样式的字符串不会尝试用户定义的转换为B以添加.为什么是这样?是否有根本的财产阻止这一点?

解决方法

在第一个例子中,允许重载解析找到您的运算符:

[C++14: 13.3.1.2/2]: If either operand has a type that is a class or an enumeration,a user-defined operator function might be declared that implements this operator or a user-defined conversion can be necessary to convert the operand to a type that is appropriate for a built-in operator. In this case,overload resolution is used to determine which operator function or built-in operator is to be invoked to implement the operator. [..]

[C++14: 13.3.2/1]: From the set of candidate functions constructed for a given context (13.3.1),a set of viable functions is chosen,from which the best function will be selected by comparing argument conversion sequences for the best fit (13.3.3). The selection of viable functions considers relationships between arguments and function parameters other than the ranking of conversion sequences.

[C++14: 13.3.2/2]: First,to be a viable function,a candidate function shall have enough parameters to agree in number with the arguments in the list.

  • If there are m arguments in the list,all candidate functions having exactly m parameters are viable.
  • [..]

[C++14: 13.3.2/3]: Second,for F to be a viable function,there shall exist for each argument an implicit conversion sequence (13.3.3.1) that converts that argument to the corresponding parameter of F. [..]

(您可以自己检查“隐式转换序列”的措辞,看看运算符电话是允许的;这些规则太冗长,无法保证在这里逐字复制.)

然而,在第二个例子中,重载分辨率被限制为一个基本的算术加法机制(一个没有为const char [N]或const char *定义),有效地禁止任何运算符函数被考虑:

[C++14: 13.3.1.2/1]: If no operand of an operator in an expression has a type that is a class or an enumeration,the operator is assumed to be a built-in operator and interpreted according to Clause 5.

[C++14: 5.7/1]: [..] For addition,either both operands shall have arithmetic or unscoped enumeration type,or one operand shall be a pointer to a completely-defined object type and the other shall have integral or unscoped enumeration type. [..]

[C++14: 5.7/3]: The result of the binary + operator is the sum of the operands.

c – 使用长度AND编码将const char *字符串转换为NSString

c – 使用长度AND编码将const char *字符串转换为NSString

我想知道哪种方法会更好地将const char * string转换为Nsstring.
我发现有一些事实.

> [Nsstring stringWithCString:length:]是不推荐使用的.
> [Nsstring stringWithCString:encoding]可能用于我的目的

但是我想要使用长度为AND的编码进行转换(因为如果通过将编码设置为UTF-8,我想使一些非ASCII字符变得更好).有什么想法吗?

我现在就想想

>使用std :: strncpy(ch1,ch2,len)创建其他字符并使用长度复制
>使用[Nsstring stringWithCString:encoding:]

但它不行.

解决方法

如果你的const char *变量被命名为foo(并且它指向一个空值终止的字符串),只需说

[Nsstring stringWithUTF8String:foo]

因为UTF-8是ASCII的超集,所以无论foo指向UTF-8还是ASCII.然后,您可以移动到完整的Unicode,没有问题.

cocoa – NSString初始化

cocoa – NSString初始化

Objective-C noob在这里.

为什么这样:

Nsstring *myString = [Nsstring alloc];
[myString initWithFormat:@"%f",storednumber];

导致以下异常 – 仅为抽象类定义的长度.定义 – [NSPlaceholderString length]!

当这工作正常:

Nsstring * myString = [[Nsstring alloc] initWithFormat:@“%f”,storednumber];

我认为后者只是前者的收缩(但我显然是错的,至少根据编译器而言).

解决方法

因为-initWithFormat:返回一个与alloc返回的对象不同的对象,即一个与myString指向的对象不同的对象.这就是你应该总是将alloc与-init耦合的原因….

这种情况在类集群(如Nsstring)中很常见. alloc返回一个通用字符串对象,然后-initWithFormat:决定Nsstring的具体子类,解除分配alloc创建的当前对象,从Nsstring的具体子类创建一个新对象,然后返回这个新对象.

cocoa – 从NSAttributedString中的nsfilewrapper / nstextattachment获取文件名和路径

cocoa – 从NSAttributedString中的nsfilewrapper / nstextattachment获取文件名和路径

我有一个基本的NSTextView,启用了丰富的文本和图形(在IB中).我想得到的是拖入的任何图像的路径和文件名,所以我可以将它们传递给另一个类.

我是NSAttributedString的新手,但我有一个使用enumerateAttributesInRange的循环:options:usingBlock:寻找NSAttachmentAttributeName,这一切都正常.但是更深入,我进入了fileWrapper类,它是apparent inability to give me the path of the item.

我如何获得NSTextAttachment的名称和路径?

相关:是否有更简单的方法来获取它们然后单步执行属性?

非常感谢!

解决方法

虽然我个人蔑视NSFileWrapper的设计,但如果您只需要每个附件的数据,您可以通过NSFileWrapper的regularFileContents方法将其作为NSData实例访问.但是,我需要一个有效且明确的路径名来指向我的应用程序的附件.获得它比应该做的要多得多:

您可以继承NSTextView并覆盖NSDraggingDestination Protocol方法draggingEntered:并且您可以在拖动操作期间遍历传递给应用程序的NSPasteboardItem对象.我选择将路径名及其inode编号保存在NSMutableDictionary中,因为NSFileWrapper可以为您提供引用文件的inode.稍后,当我通过NSAttributedString访问NSTextView内容时,我可以使用inode作为索引来获取附件的路径名.

- (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender {

    // get pasteboard from dragging operation

    NSPasteboard *pasteboard = [sender draggingPasteboard];

    NSArray *pasteboardItems = [pasteboard pasteboardItems];

    for ( NSPasteboardItem *pasteboardItem in pasteboardItems ) {

        // look for a file url type from the pasteboard item

        Nsstring *draggedURLString = [pasteboardItem stringForType:@"public.file-url"];

        if (draggedURLString != nil) {

            NSURL *draggedURL = [NSURL URLWithString:draggedURLString];

            Nsstring *draggedpath = [draggedURL path];

            NSLog(@"pathname: %@",draggedpath);

            // do something with the path

            // get file attributes

            NSDictionary *draggedAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:draggedpath error:nil];

            if ( draggedAttributes == nil)
                continue;

            // the NSFileWrapper allows access to the absolute file via NSFileSystemFileNumber
            // put the path and the inode (returned as an NSNumber) into a NSMutableDictionary 

            NSNumber *draggedInode = [draggedAttributes objectForKey:NSFileSystemFileNumber];

            [draggedFiles setobject:draggedpath forKey:draggedInode];
        }

    }

    return [super draggingEntered:sender];
}

我的解决方案的一个问题,即不影响我的应用程序,是将多个文件拖入视图(单独或一起),它们是同一文件的硬链接,只会被索引为添加到字典的最后一个路径名.分享inode.根据应用程序如何使用路径名,这可能是一个问题.

关于cocoa – 使用NSString中断NSString,获取用于中断/分离的字符串之后的所有内容的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于c – 为什么添加两个字符串文字不使用operator(const string&,const string&)?、c – 使用长度AND编码将const char *字符串转换为NSString、cocoa – NSString初始化、cocoa – 从NSAttributedString中的nsfilewrapper / nstextattachment获取文件名和路径等相关知识的信息别忘了在本站进行查找喔。

本文标签: