【问题标题】:How to get the last character and first character from a long string using `Util class`?如何使用`Util class`从长字符串中获取最后一个字符和第一个字符?
【发布时间】:2016-11-20 16:52:08
【问题描述】:

我使用下面的代码获取the last character,出现错误:

EXC_BAD_ACCESS(code=2,address=0x....)

所以我把它:

(lldb) po originStr.length
error: Trying to put the stack in unreadable memory at: 0x7fff5f010f80.
(lldb) po [originStr substringFromIndex:originStr.length - 1]
error: Trying to put the stack in unreadable memory at: 0x7fff5f010f80.

而且我的 originStr 很长: originStr 的打印说明:

(__NSCFString *) originStr = 0x00007f9e2d084c20 @"static/res/upload/headImage/2016-7-18/2d2f44bea6384d66828db17a5a7e3490.jpg,static/res/upload/headImage/2016-7-18/50269cabce1f48658ec449bc66594dbb.jpg,static/res/upload/headImage/2016-7-18/12d034621c2d4c238271b0a1abfd97a1.jpg,static/res/upload/headImage/2016-7-18/b3d3367470c843df894f8d967a5c3584.jpg,static/res/upload/headImage/2016-7-18/735b392f0f7848ae878e2c20c8e61096.jpg,static/res/upload/headImage/2016-7-18/d7192235da2c46e88e62e9c54c6eb126.jpg,static/res/upload/headImage/2016-7-18/1c7fe1c7bd5a4f0984df7a311c7bfb64.jpg,static/res/upload/headImage/2016-7-18/1471802d68ac49c7ab62973b220d17a6.jpg,static/res/upload/headImage/2016-7-18/feefe467f9564a3ea775b945d54b4f4e.jpg,static/res/upload/headImage/2016-7-18/b4c36ecd16cb41c0a86187ee78835ebb.jpg,static/res/upload/headImage/2016-7-18/a230c5605e0a47e7a0f429471a5a19e8.jpg"

编辑:我的 originStr 已从服务器关闭,dic 是数据:

NSString *originStr = dic[@"image"];

这是 originStr 来自。

编辑:

我明白了原因,因为我使用该函数删除了Util 类中的最后一个和第一个特殊字符。我已经测试了我是否在生成long stringclass 中执行操作,它可以工作好吧,如果在Util,就会出错。

下面是我的Util's函数:

@interface Util : NSObject

+ (NSString *)removeString:(NSString *)originStr lastCharacterIfItIs:(NSString *)removeCharacter;

+ (NSString *)removeString:(NSString *)originStr firstCharacterIfItIs:(NSString *)removeCharacter;
//
+ (NSString *)removeString:(NSString *)originStr firstAndLastCharacterIfItIs:(NSString *)removeCharacter;

@end

#import "Util.h"

@implementation Util

+ (NSString *)removeString:(NSString *)originStr lastCharacterIfItIs:(NSString *)removeCharacter {
    if (originStr.length == 0) {

        return originStr;
    }

    NSString *after_str;
    NSString *last_str = [originStr substringFromIndex:[originStr length] - 1];  
    // This line above code goes wrong. It shows:Thread 1:EXC_BAD_ACCESS(code=2,address=0x7fff5eabdff8),and I tried to po the originStr.length,it shows:
    //(lldb) po originStr.length
    //error: Trying to put the stack in unreadable memory at: 0x7fff5eabdf80.

    if ([last_str isEqualToString:removeCharacter]) {

        after_str =[originStr substringWithRange:NSMakeRange(0, originStr.length -1)];

    }else {
        after_str = originStr;
    }
    return after_str;
}

+ (NSString *)removeString:(NSString *)originStr firstCharacterIfItIs:(NSString *)removeCharacter {
    if (originStr.length == 0) {

        return originStr;
    }
    NSString *after_str;
    NSString *first_str = [originStr substringToIndex:1];
    if ([first_str isEqualToString:removeCharacter]) {


        after_str = [originStr substringWithRange:NSMakeRange(1, originStr.length -1)];
    }else {
        after_str = originStr;
    }

    return originStr;
}

+ (NSString *)removeString:(NSString *)originStr firstAndLastCharacterIfItIs:(NSString *)removeCharacter {

    NSString *str_last_removed = [Util removeString:originStr lastCharacterIfItIs:removeCharacter];
    return [Util removeString:str_last_removed firstAndLastCharacterIfItIs:removeCharacter];
}

@end

我认为是Util 类的内存有限,因此会导致问题。

【问题讨论】:

  • 显然问题不在于 substringFromIndex: 方法。请显示字符串是如何创建的。这可能是内存释放问题或内存不足问题。
  • 我可能同意@StanislavPankevich,我尝试创建一个 NSString 和 NSLog substringFromIndex: longString.length - 1 并且效果很好
  • @Happiehappie 我已经编辑了我的问题,我得到了导致问题的原因,但我不知道问题的真正答案是什么。
  • @StanislavPankevich 我已经编辑了我的问题,它显示了问题的更多信息,这是由 myUtil 类引起的,但我不知道真正的答案。
  • 你有关于哪条线路导致这种情况的任何信息吗?而不是希望我们检查你的整个班级

标签: ios string malloc substring


【解决方案1】:
let trimmedString: String = (address as! NSString).substringFromIndex(max(address!.length-1,0));

这是从长字符串中获取最后一个字符

【讨论】:

    【解决方案2】:

    我在操场上试过了,效果很好

    let longString = "static/res/upload/headImage/2016-7-18/2d2f44bea6384d66828db17a5a7e3490.jpg,static/res/upload/headImage/2016-7-18/50269cabce1f48658ec449bc66594dbb.jpg,static/res/upload/headImage/2016-7-18/12d034621c2d4c238271b0a1abfd97a1.jpg,static/res/upload/headImage/2016-7-18/b3d3367470c843df894f8d967a5c3584.jpg,static/res/upload/headImage/2016-7-18/735b392f0f7848ae878e2c20c8e61096.jpg,static/res/upload/headImage/2016-7-18/d7192235da2c46e88e62e9c54c6eb126.jpg,static/res/upload/headImage/2016-7-18/1c7fe1c7bd5a4f0984df7a311c7bfb64.jpg,static/res/upload/headImage/2016-7-18/1471802d68ac49c7ab62973b220d17a6.jpg,static/res/upload/headImage/2016-7-18/feefe467f9564a3ea775b945d54b4f4e.jpg,static/res/upload/headImage/2016-7-18/b4c36ecd16cb41c0a86187ee78835ebb.jpg,static/res/upload/headImage/2016-7-18/a230c5605e0a47e7a0f429471a5a19e8.jpg"
    let lastChar = longString.characters.last!
    

    如果你只在objective-c中这样做,试试

    NSString *lastChar = [state substringFromIndex: [longString length] - 1] //instead of dot notation
    

    【讨论】:

      猜你喜欢
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      • 1970-01-01
      相关资源
      最近更新 更多