{"id":6093,"library":"pyobjc-framework-datadetection","title":"PyObjC DataDetection Framework","description":"PyObjC is a bridge between Python and the Objective-C runtime, allowing Python scripts to interact with macOS frameworks. `pyobjc-framework-datadetection` provides Python bindings for the DataDetection framework on macOS, enabling programmatic detection of data such as phone numbers, dates, and addresses in text. The current version is 12.1, with releases typically aligning with new macOS SDKs and Python versions.","status":"active","version":"12.1","language":"en","source_language":"en","source_url":"https://github.com/ronaldoussoren/pyobjc","tags":["macos","cocoa","framework","pyobjc","data detection","objective-c","apple"],"install":[{"cmd":"pip install pyobjc-framework-datadetection","lang":"bash","label":"Install `pyobjc-framework-datadetection`"}],"dependencies":[{"reason":"Core bridge for PyObjC; required for all framework bindings.","package":"pyobjc-core"}],"imports":[{"symbol":"DDScanner","correct":"from DataDetection import DDScanner"},{"symbol":"DDConditionIsPhoneNumber","correct":"from DataDetection import DDConditionIsPhoneNumber"},{"symbol":"DDScannerResult","correct":"from DataDetection import DDScannerResult"},{"note":"While Python strings often bridge automatically, explicit NSString can sometimes be beneficial for clarity or specific API requirements, especially for methods expecting `NSString`.","symbol":"NSString","correct":"from Foundation import NSString"}],"quickstart":{"code":"from DataDetection import DDScanner, DDConditionIsPhoneNumber, DDConditionIsDate, DDConditionIsEmail\nfrom Foundation import NSString # Can be used for explicit Objective-C string creation, though Python strings often bridge automatically\n\n# Constants for content types (exposed directly from the DataDetection module)\nPHONE_NUMBER = DDConditionIsPhoneNumber\nDATE = DDConditionIsDate\nEMAIL = DDConditionIsEmail\n\ntext = \"My phone number is 123-456-7890 and the date is January 15, 2024. Email me at test@example.com.\"\n\n# Create a scanner for phone numbers, dates, and emails\n# The | operator is used to combine multiple conditions\nscanner = DDScanner.scannerWithContentTypes_(PHONE_NUMBER | DATE | EMAIL)\n\n# Scan the string\n# PyObjC automatically converts Python strings to NSString for Objective-C APIs\nresults = scanner.scanString_(text)\n\nprint(f\"Scanning text: '{text}'\")\nprint(\"Found results:\")\nfor result in results:\n    print(f\"  Matched String: '{result.matchedString()}'\")\n    print(f\"    Type (raw): {result.type()}\") # Raw Objective-C type constant\n    # The 'value()' method returns an Objective-C object specific to the detected type\n    if result.type() == PHONE_NUMBER:\n        print(f\"    Value (Phone Number): {result.value()}\")\n    elif result.type() == DATE:\n        print(f\"    Value (Date): {result.value()}\")\n    elif result.type() == EMAIL:\n        print(f\"    Value (Email): {result.value()}\")\n","lang":"python","description":"This quickstart demonstrates how to use `DDScanner` to detect phone numbers, dates, and email addresses in a given string. It combines multiple detection conditions using the bitwise OR operator (`|`) and then iterates through the `DDScannerResult` objects to extract matched strings and their specific values."},"warnings":[{"fix":"Upgrade Python to 3.10 or later, or use an older PyObjC version compatible with your Python environment.","message":"PyObjC has periodically dropped support for older Python versions. PyObjC 12.0 dropped Python 3.9, and PyObjC 11.0 dropped Python 3.8. Ensure your Python environment meets the `>=3.10` requirement.","severity":"breaking","affected_versions":">=11.0"},{"fix":"Carefully review custom PyObjC class implementations, especially those overriding `__new__`, to ensure `__init__` is called correctly. Refer to the PyObjC 10.3 and 10.3.1 release notes for details.","message":"Changes in `__init__` and `__new__` behavior can break custom class implementations. PyObjC 10.3 initially removed `__init__` support when `__new__` was provided by PyObjC, which was partially reverted in 10.3.1 to allow `__init__` when the user implements `__new__`.","severity":"breaking","affected_versions":"10.3, 10.3.1"},{"fix":"Understand ARC principles when working with PyObjC `init` methods, especially when manually managing object references or subclassing Objective-C classes in Python. Most common usage patterns are unaffected.","message":"PyObjC 11.1 aligned its `alloc`/`init` behavior with Clang's Automatic Reference Counting (ARC) documentation. This means methods in the 'init' family now correctly steal a reference to `self` and return a new one. This can subtly change object lifecycle behavior and may require adjustments in specific patterns of object creation and retention.","severity":"gotcha","affected_versions":">=11.1"},{"fix":"Avoid using PyObjC in a free-threaded Python 3.13 environment unless absolutely necessary and after thorough testing. Stick to regular Python threading models for stability.","message":"PyObjC's support for Python 3.13's experimental free-threading (PEP 703) is not yet fully stable or recommended. While significant changes were made in PyObjC 11.0 for experimental support, caution is advised for production use.","severity":"gotcha","affected_versions":">=10.3"},{"fix":"Upgrade to PyObjC 10.1 or later to enable `os.fspath()` compatibility with Cocoa URLs for local filesystem paths. For older versions, convert URLs to Python strings manually using appropriate PyObjC wrappers.","message":"The `os.fspath()` function will not work with `NSURL` or `CFURLRef` objects that refer to local filesystem paths in PyObjC versions prior to 10.1, raising a `TypeError` for other URLs. This was fixed in PyObjC 10.1.","severity":"gotcha","affected_versions":"<10.1"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}