Questions tagged [nsscanner]

The NSScanner class is an Objective-C class implemented in the Foundation framework, starting from Mac OS X 10.0. The NSScanner is a highly configurable tool designed for extracting substrings and numeric values from loosely demarcated strings.

The NSScanner class is an abstract superclass of a class cluster that declares the programmatic interface for an object that scans values from an NSString object.

An NSScanner object interprets and converts the characters of an NSString object into number and string values. You assign the scanner’s string on creating it, and the scanner progresses through the characters of that string from beginning to end as you request items.

Because of the nature of class clusters, scanner objects aren’t actual instances of the NSScanner class but one of its private subclasses. Although a scanner object’s class is private, its interface is public, as declared by this abstract superclass, NSScanner. The primitive methods of NSScanner are string and all of the methods listed under Configuring a Scanner. The objects you create using this class are referred to as scanner objects (and when no confusion will result, merely as scanners).

You can set an NSScanner object to ignore a set of characters as it scans the string using the charactersToBeSkipped property. Characters in the skip set are skipped over before scanning the target. The default set of characters to skip is the whitespace and newline character set.

To retrieve the unscanned remainder of the string, use [[scanner string] substringFromIndex: [scanner scanLocation]].

146 questions
29
votes
5 answers

What causes "NSScanner: nil string argument"?

I got this message when I save data to core data. NSScanner: nil string argument I didn't use any NSScanner method. Where did it come from? This is a bug? What should I do with it? Thanks help, please.
Jimi
  • 1,091
  • 1
  • 14
  • 19
25
votes
2 answers

Best way to split strings into an array

I'm developing a travel app, I have to read a txt file that contains all the states and countries, as you can notice this is a pretty huge file to read, anyway, this is an example of the text inside the file: Zakinthos (ZTH),GREECE Zanesville…
Rafael Jimeno
  • 626
  • 2
  • 8
  • 20
19
votes
5 answers

How to use NSScanner?

I've just read Apple documentation for NSScanner. I'm trying to get the integer of this string: @"user logged (3 attempts)" I can't find any example, how to scan within parentheses. Any ideas? Here's the code: NSString *logString = @"user logged (3…
Zopi
  • 235
  • 1
  • 3
  • 7
11
votes
1 answer

Strange behaviour of NSScanner on simple whitespace removal

I'm trying to replace all multiple whitespace in some text with a single space. This should be a very simple task, however for some reason it's returning a different result than expected. I've read the docs on the NSScanner and it seems like it's…
Michael Waterfall
  • 20,497
  • 27
  • 111
  • 168
9
votes
2 answers

Optimize String Parsing

I have a requirement to parse data files in "txf" format. The files may contain more than 1000 entries. Since the format is well defined like JSON, I wanted to make a generic parser like JSON, which can serialise and deserialise txf files. On…
Anupdas
  • 10,211
  • 2
  • 35
  • 60
9
votes
1 answer

Does NSScanner work with Swift Strings?

I was wondering why this works (notice I am using NSString types): let stringToSearch:NSString = "I want to make a cake and then prepare coffee" let searchTerm:NSString = "cake" let scanner = NSScanner(string: stringToSearch) var…
Narwhal
  • 744
  • 1
  • 8
  • 22
7
votes
2 answers

find all numbers in a string with a NSScanner

I used the below code to extract numbers from inputString using NSScanner NSString *inputString = @"Dhoni7 notout at183*runs in 145andhehit15four's and10sixers100"; NSString *numberString; NSArray *elements = [inputString…
Manju Basha
  • 665
  • 1
  • 9
  • 29
6
votes
1 answer

Using NSScanner to parse a string

I have a string with formatting tags in it, such as There are {adults} adults, and {children} children. I have a dictionary which has "adults" and "children" as keys, and I need to look up the value and replace the macros with that value. This is…
GendoIkari
  • 11,734
  • 6
  • 62
  • 104
5
votes
1 answer

How to use NSScanner to parse .ics file

Could someone please show how i could parse a ics file using NSScanner? (Iphone App) e.g: if the .ics file was at this URL http://www.ibz.com/data/12345.ics (not a real URL!!!) How would i firstly save the .ics file into my iphone app and then…
Ibz
  • 518
  • 1
  • 8
  • 26
5
votes
1 answer

Handling CGFloat with an NSScanner on arm64

Apparently CGFloat is double on arm64: #if defined(__LP64__) && __LP64__ # define CGFLOAT_TYPE double # define CGFLOAT_IS_DOUBLE 1 # define CGFLOAT_MIN DBL_MIN # define CGFLOAT_MAX DBL_MAX #else # define CGFLOAT_TYPE float # define CGFLOAT_IS_DOUBLE…
A-Live
  • 8,904
  • 2
  • 39
  • 74
5
votes
2 answers

Convert NSData into Hex NSString

With reference to the following question: Convert NSData into HEX NSString I have solved the problem using the solution provided by Erik Aigner which is: NSData *data = ...; NSUInteger capacity = [data length] * 2; NSMutableString *stringBuffer =…
Dawson
  • 117
  • 1
  • 9
4
votes
2 answers

How to use NSScanner to scan in the format of ^[a-zA-Z_][a-zA-Z0-9_]*

I'm trying to have my NSScanner attempt to scan the following regexp: [a-zA-Z_][a-zA-Z0-9_]*, but am having difficulty. I can try to read a-Z_ first, then try to append a-Z0-9_. I'm wondering if there is an easier / more efficient way of doing…
AWF4vk
  • 5,810
  • 3
  • 37
  • 70
4
votes
2 answers

Replace numeric of different length in a string with different text length

Problem: Numeric is of different length could be 1, 200, 1000, 39 99995 etc. And need to replace with a text eg. "Apple" which is of different lenth comapring to the numeric values. let str: String = "hello i have 1313 object of 10 string class with…
Pankaj Bhardwaj
  • 2,081
  • 1
  • 17
  • 37
4
votes
4 answers

Efficient NSString parsing with NSScanner

To begin, I am writing an iOS 5 app. By way of example, say that I have the following string: 100 - PARK STREET / JAMES PLACE I would like to extract the two road names from this string in the most efficient (and code-elegant) way possible. I have…
Skoota
  • 5,280
  • 9
  • 52
  • 75
3
votes
1 answer

iOS/Objective-C: Attempting to Scan a string for substrings which will be assigned to multiple NSStrings

I'm attempting to complete the Stanford iPhone Programming (FA10) assignement "Flickr Fetcher" -- so far things are going well, however I have come to an impasse: I have successfully extracted the location of the "Top 100" pictures, which are…
Maximilian
  • 1,107
  • 11
  • 20
1
2 3
9 10